PDO MYSQL使用PHP准备的Update语句未执行
我试图在MySQL数据库上通过PHP使用PDO执行一个准备好的语句 我尝试了两个版本的代码,但都不起作用。将执行函数更新,但数据库中不会更新任何内容。使用fetch()和fetchAll()的视图customerData函数与deleteData函数一样工作 我当前的数据库结构是:PDO MYSQL使用PHP准备的Update语句未执行,php,mysql,pdo,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(varc
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条准备好的语句工作得很好,例如,这里是填充updatecustomer表单的准备好的语句
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();
}
}
函数视图\u customerData($customerID){
全球$db;
$query=“从客户中选择*
其中customerID=$customerID”;
试一试{
$statement=$db->prepare($query);
$statement->execute();
$customerData=$statement->fetch();
返回$customerData;
}捕获(PDO$e){
$error_message=$e->getMessage();
echo“数据库错误:$error_message”;
退出();
}
}
尝试将整个更新客户代码放在Try
块上,如果出现任何错误,则放在catch
块上。但首先要修正这条线
$statement->bindValue(':pass', $password);
到
$statement->bindValue(':password',$password);
^^^^
试一试{
//…将您的更新客户代码放在此处。。。
}捕获(PDO$e){
$error_message=$e->getMessage();
echo“数据库错误:$error_message”;
退出();
}
将customerID的参数指定为整数以确保安全$statement->bindValue(':ID',$customerID,PDO::param_INT);注意您在密码上使用的占位符不匹配,因为@Being Sunny突出显示。
$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();
}