Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL INSERT not inserting nor any error is displayed

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

like image 903
fftoolbar Avatar asked Jan 08 '13 23:01

fftoolbar


3 Answers

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.

like image 140
noderman Avatar answered Oct 13 '22 01:10

noderman


Check the mysqli::$errno first.

if(mysql->errno==0)
   echo "successful"
else
   echo mysql->error;
like image 33
Shiplu Mokaddim Avatar answered Oct 13 '22 02:10

Shiplu Mokaddim


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;
like image 33
Joshua G Avatar answered Oct 13 '22 00:10

Joshua G