I need a query to execute when entering a page (php). I've already added this to the top.
Code:-
mysql_query("INSERT INTO `table_name` (`player_id`, `unit1`, `unit2`, `unit3`, `unit4`)
VALUES ('".$_SESSION['user']['id']."', 0, 0, 0, 0)");
When my users enter the page that contains that, the query should be executed. Yes database connection is included.
How do I fix this? (also will it duplicate player_id)?
First mysql
is deprecated use mysqli
.
Your query is fine how i can see, but your table could be a problem.
You should not have primary key on player_id
if you wish to duplicate player_id
. (Otherwise keep the primary key).
Also if you wish to have one player_id
with those results (not multiple / duplicates), then you could create query in this way:
$query = "INSERT INTO `table_name` (`player_id`, `unit1`, `unit2`, `unit3`, `unit4`)
VALUES ('".$_SESSION['user']['id']."', 0, 0, 0, 0)
ON DUPLICATE KEY UPDATE `unit1`=0, `unit2`=0, `unit3`=0, `unit4`=0";
UPDATE:
Also there could be a problem that you are missing a database identifier at mysql_query
:
mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )
like mysql_query("MYQUERY", $mylinkISavedAtConnectingToDatabase);