简体   繁体   中英

PDO MYSQL Prepared Update Statement using PHP Not executing

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM