本文介绍了使用 PDO 和 MySQL 更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 PDO 编写更新查询,但我无法执行代码?
Im trying to write an update query with PDO only I cant get my code to execute?
try {
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE `access_users`
(`contact_first_name`,`contact_surname`,`contact_email`,`telephone`)
VALUES (:firstname, :surname, :telephone, :email);
";
$statement = $conn->prepare($sql);
$statement->bindValue(":firstname", $firstname);
$statement->bindValue(":surname", $surname);
$statement->bindValue(":telephone", $telephone);
$statement->bindValue(":email", $email);
$count = $statement->execute();
$conn = null; // Disconnect
}
catch(PDOException $e) {
echo $e->getMessage();
}
推荐答案
- 您的
UPDATE
语法错误 - 您可能打算更新一行而不是全部,因此您必须使用
WHERE
子句来定位您的特定行
- Your
UPDATE
syntax is wrong - You probably meant to update a row not all of them so you have to use
WHERE
clause to target your specific row
改变
UPDATE `access_users`
(`contact_first_name`,`contact_surname`,`contact_email`,`telephone`)
VALUES (:firstname, :surname, :telephone, :email)
到
UPDATE `access_users`
SET `contact_first_name` = :firstname,
`contact_surname` = :surname,
`contact_email` = :email,
`telephone` = :telephone
WHERE `user_id` = :user_id -- you probably have some sort of id
这篇关于使用 PDO 和 MySQL 更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!