PDO更新多条记录

时间:2013-05-07 22:37:14

标签: 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或多行更新有关。

提前致谢。

致Sam:

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 )

1 个答案:

答案 0 :(得分:5)

:description之后还有一个额外的逗号,应该是:

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