2013-05-07 61 views
1

我知道這個問題之前已經被問過了,並且我已經通過了很多答案,並正在關閉其中一個answers now,但是需要一些幫助來處理以下代碼塊。PDO正在更新多個記錄

<?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) 
+0

是否'的print_r($ STH-> errorInfo());'輸出什麼? – Sam 2013-05-07 22:48:38

+0

我向那裏的問題添加了錯誤,謝謝 – Owen 2013-05-07 22:52:18

回答

5

你有:description之後額外的逗號,它應該是:

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

...我現在對自己很失望......但是,謝謝 – Owen 2013-05-07 23:07:58

+1

這是一個很容易錯過的問題! – Sam 2013-05-07 23:11:31