1

I have recently been working with PDO, so I am new to it and for some reason I can not get this to insert records into the database. What am I doing wrong here!?!?

if (isset($_POST['submit'])) {
    $sql = "INSERT INTO customer(firstname,lastname,email,address,city,state,zip,phone,company)
VALUES(:firstname,:lastname,:email,:address,:city,:state,:zip,:phone,:company)";

    $stmt = $db->prepare($sql);

    if (!$_POST['fname'])
        $errors[] = 'You must input a first name';
    if (!$_POST['lname'])
        $errors[] = 'You must input a last name';
    if (!$_POST['address'])
        $errors[] = 'You must input an address';
    if (!$_POST['city'])
        $errors[] = 'You must input a city';
    if (!$_POST['state'])
        $errors[] = 'You must input a state';
    if (!$_POST['zip'])
        $errors[] = 'You must input a zip';
    if (!$_POST['email'])
        $errors[] = 'You must input a e-mail';
    if (!$_POST['phone'])
        $errors[] = 'You must input a phone';

    if (!$errors) {
        $stmt->bindParam(':firstname', $_POST['fname'], PDO::PARAM_STR);
        $stmt->bindParam(':lastname', $_POST['lname'], PDO::PARAM_STR);
        $stmt->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
        $stmt->bindParam(':address', $_POST['address'], PDO::PARAM_STR);
        $stmt->bindParam(':city', $_POST['city'], PDO::PARAM_STR);
        $stmt->bindParam(':state', $_POST['state'], PDO::PARAM_STR);
        $stmt->bindParam(':zip', $_POST['zip'], PDO::PARAM_STR);
        $stmt->bindParam(':phone', $_POST['phone'], PDO::PARAM_STR);
        $stmt->bindParam(':company', $_POST['company'], PDO::PARAM_STR);

        $stmt->execute();
    }
} 

When I do print_r($db->errorInfo()); I get this array returned to me upon submitting

Array ( [0] => 00000 [1] => [2] => ) 1
Steve
  • 20,703
  • 5
  • 41
  • 67
Conner Burnett
  • 502
  • 1
  • 11
  • 24

1 Answers1

2

From http://www.php.net/manual/en/pdo.errorinfo.php:

PDO::errorInfo() only retrieves error information for operations performed directly on the database handle. If you create a PDOStatement object through PDO::prepare() or PDO::query() and invoke an error on the statement handle, PDO::errorInfo() will not reflect the error from the statement handle. You must call PDOStatement::errorInfo() to return the error information for an operation performed on a particular statement

So, to find the actual error, use $stmt->errorInfo(). If that information doesn't help you further, please add the error you got to the question or ask a new one.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Thanks! Once I used the specific error I got the information I was needing. My spelling was a little off on one of the words ;) – Conner Burnett Apr 02 '14 at 14:25
  • 1
    Posting full code expedites things. I had a feeling that's what it was. @ConnerBurnett Where I was going to add to check for letter-case/spelling in my posted comments under your question. Next time, should there be a next time, post full code. It takes the guesswork out of things ;-) – Funk Forty Niner Apr 02 '14 at 14:32
  • 1
    @Fred-ii- That's right, although without a proper error message, it's still hard to guess. That's why, this time, I took the phrase `"When I do print_r($db->errorInfo()); I get this [unhelpful] array returned to me upon submitting"` and used that for a problem description. Knowing how to get proper error information is in the end more helpful than letting people guess the error by showing them all your code everytime you're stuck, and I'm happy that Conner was able to solve his own problem in the end. – GolezTrol Apr 02 '14 at 16:07
  • 1
    @GolezTrol I agree. Using the right error reporting functions, one can pinpoint the error. OP's often leave out the (*most*) important stuff in order to properly diagnose the problem. I often post a comment to the affect of `A!=a;` ;-) Cheers – Funk Forty Niner Apr 02 '14 at 16:10
  • @Fred-ii- Maybe I would have done that too, if I had spotted it. ;-) – GolezTrol Apr 02 '14 at 16:17
  • Just like the old VCR's, too many moving parts can go bad, then try to find that tiny defective bearing that's munching up your valuable tapes! lol @GolezTrol – Funk Forty Niner Apr 02 '14 at 16:22