php - PDO 更新多条记录

标签 php mysql pdo sql-update

我知道以前有人问过这个问题,我已经看过很多答案并且正在处理其中一个 answers now ,但是需要以下代码块的帮助。

<?php

$title = $_POST['title'];
$description = $_POST['description'];
$item_name = $_POST['item_name'];

$A = count($item_name);

include ("connection.php");

try {

    $set_details = "UPDATE images
                    SET title = :title,
                    description = :description,
                    WHERE item_name = :item_name";


$STH = $conn->prepare($set_details);

    $i = 0;
    while($i < $A) {
        $STH->bindParam(':title', $title[$i]);
        $STH->bindParam(':description', $description[$i]);
        $STH->bindParam(':item_name', $item_name[$i]);
        $STH->execute();
        $i++;
    }
}
catch(PDOException $e) {  
    echo "I'm sorry, but there was an error updating the database.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}


?>

我在执行时没有出现任何错误,也没有任何内容提交到 mysql 表,如果您发现了什么请告诉我,或者如果有更好的方法来解决这个问题,您能指点我看教程吗,我还没有与 PDO 或多行更新一起工作很多。

提前致谢。

致山姆:

print_r($STH->errorInfo());

输出是:

Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE item_name = '27'' at line 4 )

最佳答案

:description后面多了一个逗号,应该是:

"UPDATE images
 SET title = :title,
 description = :description
 WHERE item_name = :item_name"

关于php - PDO 更新多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16429736/

相关文章:

mysql - SQL 查询耗时超过 1 小时

php - PDO->execute() 语句将不起作用并且在语法上是正确的

php - 使用 PDO 的更新语句是否根据 setFetchMode 准备返回数据?

PHP MySQL JSON 登录系统

php - 使用 MySQL 查询将特定值分配给数组元素

PHP 警告:mysqli::query():无法获取 mysqli

php - 使用 mysql PDO 插入和删除标签

PHP/MySQL/JSON 编码帮助

php - 通过 PHP 伪造一个 $_POST

php - 转义 PHP PDO 中的预设列名