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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!