I am having difficulties troubleshooting some simple PHP code to insert a record in a MySQL table.
This code entered directly into WAMP works fine:
INSERT INTO `users` (`userName`,`userEmail`) VALUES ('orange','orange@gmail.com')
This PHP code doesn't work:
<?php
$dbHost="localhost";
$dbName="project";
$dbUser="admin";
$dbPassword="abcd";
$dbh=new PDO("mysql:host=$dbHost;dbName=$dbName", $dbUser, $dbPassword);
print_r($dbh);
echo "</br>";
print_r($dbh->errorInfo());
$query=$dbh->prepare("INSERT INTO users (userName, userEmail) VALUES (?,?)");
echo "</br>";
print_r(var_dump($query->errorInfo()));
echo "</br>";
print_r($query->errorCode());
echo "</br>";
print_r($dbh->errorInfo());
$query->bindValue(1, 'apple');
echo "</br>";
print_r(var_dump($query->errorInfo()));
echo "</br>";
print_r($query->errorCode());
echo "</br>";
print_r($dbh->errorInfo());
$query->bindValue(2, 'apple@gmail.com');
echo "</br>";
print_r(var_dump($query->errorInfo()));
echo "</br>";
print_r($query->errorCode());
echo "</br>";
print_r($dbh->errorInfo());
$inserted=$query->execute(); //True if succesful, False if not.
echo "</br>";
print_r(var_dump($query->errorInfo()));
echo "</br>";
print_r($query->errorCode());
echo "</br>";
print_r($dbh->errorInfo());
echo "</br>";
if ($inserted){print_r("true");}else{print_r("false");};
?>
What I get when I execute the page is the following printout:
PDO Object ( )
Array ( [0] => [1] => [2] => )
array(3) { [0]=> string(0) "" [1]=> NULL [2]=> NULL }
Array ( [0] => 00000 [1] => [2] => )
array(3) { [0]=> string(0) "" [1]=> NULL [2]=> NULL }
Array ( [0] => 00000 [1] => [2] => )
array(3) { [0]=> string(0) "" [1]=> NULL [2]=> NULL }
Array ( [0] => 00000 [1] => [2] => )
array(3) { [0]=> string(5) "3D000" [1]=> int(1046) [2]=> string(20) "No database selected" }
3D000
Array ( [0] => 00000 [1] => [2] => )
false
The record isn't inserted in the db. What I am doing wrong? I am not sure what I should see in the print_r's, I am providing them as help for responders.
Thank you,
JDelage
edited - I added the print_r's recommended in the comments.
Here is what I see in WAMP: