6

I have got this code so insert values into a table in MySQL through PHP. I have tried all the possible Insert syntax, it does not insert the data... this are the codes that i used.

$param = "xyzxyz";
$param1 = "sdfdfg";
$sql = "INSERT INTO trail (User_Name, Quiz_ID) VALUES ('".$param."','".$param1."')";
$result = $mysql->query($sql);
if($result)
   echo "successful";
else
   echo mysql->error;
if(mysql->errno==0)
   echo "successful"
else
   echo mysql->error;

I even tried the following sql syntax

"INSERT INTO trail (User_Name, Quiz_ID) VALUES ('$param1','$param1')";

"INSERT INTO `trail` (`User_Name`, `Quiz_ID`) VALUES ('$param1','$param1')";

and i tried several other none of them inserts anything into the table. and this is the table in MySQL;

trail

User_Name varchar(35)
Quiz_ID varchar(35)

It does not insert anything nor does it display any error. And I have the correct DB connection because i am able to Select from the table. Its just the insert that is tricky.

Any help would be much appreciated.

Thanks

fftoolbar
  • 175
  • 1
  • 1
  • 11
  • 1
    Maybe no insert rights on table? – boruch Jan 08 '13 at 23:59
  • 1
    Are you using mysqli or mysql? – Shiplu Mokaddim Jan 09 '13 at 00:00
  • Also in the code it says trails while the table is trailanswer – boruch Jan 09 '13 at 00:01
  • Try to find error message. Maybe in the log or with php function `mysql_error()` – Ajax Jan 09 '13 at 00:04
  • maybe your vars are larger than VARCHAR(35) ? – v0d1ch Jan 09 '13 at 00:06
  • First, does it print *successful* and secondly, how are you asserting the values are **not** inserted? Are you looking at the correct database and table? – Phil Jan 09 '13 at 00:38
  • @phil it is printing successful and since i deleted all the values in the table so i am using SELECT*. And i checked both on MySQL console and and even on PHPMYAdmin. And yes the table name is correct on the correct database. All those checks done. Thanks – fftoolbar Jan 09 '13 at 01:13
  • 1
    One idea: How about manually inserting some data and just trying to SELECT it in code? Does that work? I.e., are you even connecting to the database and table. definitely a challenging problem, it seems. Thanks for cleaning up the sample code, that helped. – DWright Jan 09 '13 at 06:06

7 Answers7

4

Just a note if someone is running on similar problems:

I had a similar issue --- Insert query working on PHPMyAdmin but not working on PHP and not issuing any errors (result was true all the time).

The reason is that I was starting a transaction but forgetting to commit it...

$mysqli->autocommit(FALSE);

$mysqli->query( "START TRANSACTION" );

Never forget this:

$mysqli->commit();

It is a silly error, I know, but I was so focused on the query mistery that I forgot the transaction statements a few lines above.

noderman
  • 1,934
  • 1
  • 20
  • 36
3

Check the mysqli::$errno first.

if(mysql->errno==0)
   echo "successful"
else
   echo mysql->error;
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
1

What I have done is if you don't have a debugger installed, just have it email you the query. This way you can see what the final query is and if you have access to something like phpMyAdmin try manually running the query and see what happens. Another thing, make sure that you are searching for your inserted record correctly, if you are using a search query because of the number of records make sure the WHERE condition is right, that has burned me a few times.

EDIT Missing symbol around names maybe. I have to run all my MySQL queries like

`nameOfThing`

instead of just nameOfThing

$param = "xyzxyz";
$param1 = "sdfdfg";
$sql = "INSERT INTO `trail` (`User_Name`, `Quiz_ID`) VALUES ('".$param."','".$param1."')";
$result = $mysql->query($sql);
if($result)
   echo "successful";
else
   echo mysql->error;
if(mysql->errno==0)
   echo "successful"
else
   echo mysql->error;
Joshua G
  • 2,086
  • 3
  • 19
  • 22
0

FYI, you are inserting $param1 twice.

You also don't have a ';' after echo "successful".

I'd suggest you clean up the code example, and try things again, and let us know.

Things to clean up

  1. $sql = "INSERT INTO trail (User_Name, Quiz_ID) VALUES ('$param','$param1')"; You don't need to concatenate the variables in a string concatenate, you can interpolate. However, you actually should use PDO with a prepared statement to avoid the potential for SQL injection.
  2. Add that missing ;
  3. put that first check of if(mysql->errno==0) in (unless you are going to switch to PDO for this stuff).
  4. Fix mysql->error to be mysql->error()
  5. Maybe some other things from the comments.
DWright
  • 9,258
  • 4
  • 36
  • 53
  • yes tht was typo too...but even even i enter $param and $param1 doesnot insert anything. And i dont think inserting the same value shouldn't be a problem anyways. thanks – fftoolbar Jan 09 '13 at 00:13
  • I updated my answer to summarize a few things that need fixing. Not sure these will solve your problem, but until you fix them, you're not going to progress in finding the real problem, especially with people here trying to help, based on uncorrected code. – DWright Jan 09 '13 at 00:25
  • 1
    You may be thinking: "why fix these, if they are not **the** fix"? But until you do you're like someone asking for directions in the city with a bunch of ketchup on their face. People ought to be giving you directions in the city, but they are completely distracted by the ketchup. If you clean up the code (wipe off the ketchup) people can focus on the real issues. – DWright Jan 09 '13 at 00:34
  • yes i am a seasoned MYSQL programmer so those syntaxes are all fixed it was just a typo when i was copying the code onto this forum.. so those syntaxes are all working correctly without any error and select and all the other mysql command works except this drama with insert and this is the firs time i am experiencing this weird much – fftoolbar Jan 09 '13 at 01:03
0

Well, if the following code produce no error and shows 1 affected row, most likely you are looking for the result in the wrong database.

ini_set('display_errors', 1);
error_reporting(E_ALL);

$sql = "INSERT INTO trail (User_Name, Quiz_ID) VALUES ('testing','1')";
$mysql->query($sql);
var_dump($mysql->error,$mysql->affected_rows);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

My tables were InnoDB tables and when i changed my tables to MyISAM the insert worked fine. Well i have never encountered this problem before. Well that did the trick for the time being.

If i want to use InnoDB engine for transactions? How can i get php to be able to insert values in InnoDB table? Any one got any suggestion? And i am using WAMP server and the MySQL is version 5.5.24. And i did change the InnoDB conf in my.ini but that did not seem to work either?

fftoolbar
  • 175
  • 1
  • 1
  • 11
-1

try this

$param = "xyzxyz"; 
$param1 = "sdfdfg"; 
$sql = "INSERT INTO trail (User_Name, Quiz_ID) VALUES ('".$param."','".$param1."')"; $result = $mysql_query($sql); if($result){ echo "successful";} else { echo " not successful;}

Ankur
  • 5,086
  • 19
  • 37
  • 62
dubey
  • 1