-4

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)?

Bibhudatta Sahoo
  • 4,808
  • 2
  • 27
  • 51
Omega Cebbo
  • 17
  • 2
  • 7
  • 1
    are you getting any error on running that? – Suresh Kamrushi Jul 05 '17 at 10:50
  • No, I see nothing at all. I've check chrome console and it doesn't display anything about the query, should I use ajax maybe? – Omega Cebbo Jul 05 '17 at 10:51
  • if 'player_id' is primary key then you can not insert more than once with the same User_id... – Suresh Kamrushi Jul 05 '17 at 10:52
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jul 05 '17 at 11:21

1 Answers1

0

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);

Ultrazz008
  • 1,678
  • 1
  • 13
  • 26
  • player_id is on primary key, but it's so weird it doesn't execute the query. – Omega Cebbo Jul 05 '17 at 10:55
  • At those information you gave to us, it's okay, it should execute but there's possibility to have no affected rows because of duplicate primary key. Also there could be problem with connecting to database, or the table_name is invalid or you entered wrong parameters, or there's missing parameter that is needed (NOT NULL). – Ultrazz008 Jul 05 '17 at 10:58
  • Also you could check why it's not execute with `mysql_error`.. http://php.net/manual/en/function.mysql-error.php – Ultrazz008 Jul 05 '17 at 11:00
  • @OmegaCebbo Check for update i posted within this answer, it may solve your problem... – Ultrazz008 Jul 05 '17 at 11:05
  • Can you advance "$mylinkISavedAtConnectingToDatabase", what should it say there? – Omega Cebbo Jul 05 '17 at 11:06
  • If you were connected to database like this `$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')` you should pass at `mysql_query("MYQUERY", $link);` the link of connection. – Ultrazz008 Jul 05 '17 at 11:07
  • I just asked because I don't want the query to create duplicates. And btw, player_id is primary key. – Omega Cebbo Jul 05 '17 at 11:07
  • Now, the end of the query looks like this. ('".$_SESSION['user']['id']."', 0, 0, 0, 0)", $con); is it correct? – Omega Cebbo Jul 05 '17 at 11:11
  • No success so far, I know that my connection is correct. How can I display errors? – Omega Cebbo Jul 05 '17 at 11:17
  • Use `mysql_query("YOURQUERY") or die ("Error in query: ".mysql_error());` – Ultrazz008 Jul 05 '17 at 11:19