使用PHP的PDO MYSQL准备更新语句未执行

时间:2015-04-26 14:00:52

标签: php mysql pdo

我正在尝试在MySQL数据库上通过PHP使用PDO执行预准备语句。

我试过两个版本的代码都没有用。函数更新将执行,但数据库中不会更新任何内容。我使用fetch()和fetchAll()查看customerData函数的工作方式和deleteData函数一样。

我目前的数据库结构是:

customerID(int11)
firstName(varchar(50)
lastName(varchar(50)
address(varchar(50)
city(varchar(50)
state(varchar(50)
postalCode(varchar(20)
countryCode(char(2)
phone(varchar(20)
email(varchar(50)
password(varchar(20)

我正在使用的当前版本的代码:

function update_customer($customerID, $firstName, $lastName, $address, $city, $state, $postalCode, $countryCode, $phone, $email, $password)
{   
  global $db;
    $query = "UPDATE customers
                  SET
                      firstName = :first,
                      lastName = :last,
                      address = :add,
                      city = :c,
                      state = :s,
                      postalCode = :postal,
                      countryCode = :country,
                      phone = :p,
                      email = :e,
                      password = :password
                  WHERE customerID = :ID";
    $statement = $db->prepare($query);
    $statement->bindValue(':first',$firstName);
    $statement->bindValue(':last', $lastName);
    $statement->bindValue(':add', $address);
    $statement->bindValue(':c' ,$city);
    $statement->bindValue(':s',$state);
    $statement->bindValue(':postal', $postalCode);
    $statement->bindValue(':country',$countryCode);
    $statement->bindValue(':p', $phone);
    $statement->bindValue(':e', $email);
    $statement->bindValue(':pass', $password);
    $statement->bindValue(':ID', $customerID);
    $statement->execute();
    $statement->closeCursor();
}

我使用的其他版本的代码

function update_customer($customerID, $firstName, $lastName, $address, $city, $state, $postalCode, $countryCode, $phone, $email, $password)
{
    global $db;
    $query = "UPDATE customers
                  SET
                      firstName = ?,
                      lastName = ?
                      address = ?,
                      city = ?,
                      state = ?,
                      postalCode = ?,
                      countryCode = ?,
                      phone = ?,
                      email = ?,
                      password = ?
                  WHERE customerID = ?";
    $statement = $db->prepare($query);
    $statement->bindParam('ssssssssssi', $firstName, $lastName, $address, $city, $state, $postalCode, $countryCode, $phone, $email, $password, $customerID);
    $statement->execute();
    $statement->closeCursor();
}

我的其他3个预准备语句完美运行,例如,这里是填充更新客户表单的预准备语句。

function view_customerData ($customerID) {
    global $db;
    $query = "SELECT * FROM customers
                  WHERE customerID = $customerID";
    try {
        $statement = $db->prepare($query);
        $statement->execute();
        $customerData = $statement->fetch();
        return $customerData;
    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        echo "<p>Database error: $error_message </p>";
        exit();
    }
}

1 个答案:

答案 0 :(得分:2)

尝试将整个更新客户代码放在try块上,如果发生任何错误,请将catch阻止。但首先要解决这条问题

$statement->bindValue(':pass', $password); 

 $statement->bindValue(':password', $password); 
                             ^^^^

try {
 //.....put your update customer code here ...
} catch (PDOException $e) {
     $error_message = $e->getMessage();
    echo "<p>Database error: $error_message </p>";
    exit();
}