PDO Insert不会添加到DB

时间:2014-04-02 14:10:33

标签: php mysql pdo

我最近一直在使用PDO,所以我是新手,因为某些原因我无法将此记录插入数据库。我在这里做错了什么!?!?

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();
    }
} 

当我执行print_r($ db-> errorInfo());我在提交

后将这个数组返回给我
Array ( [0] => 00000 [1] => [2] => ) 1

1 个答案:

答案 0 :(得分:2)

来自http://www.php.net/manual/en/pdo.errorinfo.php

  

P DO::errorInfo()仅检索直接在数据库句柄上执行的操作的错误信息。如果通过PDO :: prepare()或PDO :: query()创建PDOStatement对象并在语句句柄上调用错误,PDO :: errorInfo()将不会反映语句句柄中的错误。您必须调用PDOStatement::errorInfo()以返回对特定语句执行的操作的错误信息

因此,要查找实际错误,请使用$stmt->errorInfo()。如果该信息对您没有任何帮助,请将您收到的错误添加到问题中或提出新问题。