duanlaofu4108 2015-04-26 14:00
浏览 86
已采纳

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

I am trying to execute a prepared statement using a PDO via PHP on a MySQL database.

I have tried two versions of the code both have not worked. The function update will execute but nothing will get updated in the database. My view customerData functions using fetch() and fetchAll() both work as does my deleteData function.

My current database structure is:

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)

The current version of code I am using:

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

The other version of code I have used

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

My other 3 prepared statements work perfectly, for example here is the prepared statement that populates the update customer form.

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条回答 默认 最新

  • duan19740319 2015-04-26 14:16
    关注

    Try to put the whole update customer code on try block and put catch block if any error occurs. But first of all fix this line

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

    to

     $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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失