3

Hey, so I am new with PDO, and I cannot figure out why my data will not insert into my tables. Much appreciated!

<?php

include("class.php");

$dbhost     = "localhost";
$dbname     = "db";
$dbuser     = "user";
$dbpass     = "pass";

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

$username = $_POST['username'];
$password = $_POST['password'];
$email = strtolower($_POST['email']);
$firstName = ucwords(strtolower($_POST['firstName']));
$lastName = ucwords(strtolower($_POST['lastName']));
$date = date("Y-m-d");
$hash = Secure::Encrypt($username, $password);

$sql = "INSERT INTO users (username,password,email,firstName,lastName,createDate) VALUES (:username,:password,:email,:firstName,:lastName,:date)";
$q = $conn->prepare($sql);
$q->execute(array(
':username'=>$username,
':password'=>$hash,
':email'=>$email,
':firstName'=>$firstName,
':lastName'=>$lastName,
':date'=>$date));

?>
Chris
  • 461
  • 3
  • 9
  • 16
  • 1
    Your question title says that you're getting a connection failure, but your question body does not mention a specific problem. Can you please provide more details, like the full and complete wording of any errors you're getting? – Charles Mar 25 '11 at 00:53
  • 4
    Wrap your PDO code with a `try` block, with a catch for `PDOException`. Then get the exception message and include it in the question. –  Mar 25 '11 at 00:54
  • 2
    This may not be relative to your issue, but I am surprised how many people don't do this as it helps save debug time, esp with PDO. I tend to always check my queries manually. If you have control of the server, ie a dev box etc. you may want to turn on mysql logging. If your on linux, e.g., /etc/mysql/my.cnf look for the log section, uncomment that, restart mysql /etc/init.d/mysql restart .. then as root (sudo -i) do this: tail -f /var/log/mysql/mysql.log | grep INSERT Remember to turn off mysql.log after your done. This file gets huge fast. – mardala Mar 26 '11 at 01:44

1 Answers1

4

If all of your table's field names match, I have found that PDO will sometimes fail if one of your array variables are empty.

to find out if any errors are being thrown, add the following after your new PDO declaration (PDO error reporting is silent by default):

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

Then add try/catch around each PDO activity as follows:

try {
    ..Code Here...
}catch (PDOException $err) {
    echo $err->getMessage();
}

Final Code:

<?php

include("class.php");

$dbhost     = "localhost";
$dbname     = "db";
$dbuser     = "user";
$dbpass     = "pass";

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$username = $_POST['username'];
$password = $_POST['password'];
$email = strtolower($_POST['email']);
$firstName = ucwords(strtolower($_POST['firstName']));
$lastName = ucwords(strtolower($_POST['lastName']));
$date = date("Y-m-d");
$hash = Secure::Encrypt($username, $password);

$sql = "INSERT INTO users (username,password,email,firstName,lastName,createDate) VALUES (:username,:password,:email,:firstName,:lastName,:date)";
try {    
    $q = $conn->prepare($sql);
 }catch (PDOException $err) {
    echo 'Prepare Failed: '.$err->getMessage();
 }
try {    
    $q->execute(array(
    ':username'=>$username,
    ':password'=>$hash,
    ':email'=>$email,
    ':firstName'=>$firstName,
    ':lastName'=>$lastName,
    ':date'=>$date));
 }catch (PDOException $err) {
    echo 'Execute Failed: '.$err->getMessage();
 }

?>

Then please update with any errors you receive :)

MaurerPower
  • 2,046
  • 7
  • 26
  • 48