2017-01-23 28 views
0

我是新來的php &真的迷路了。我正在嘗試使用多個陣列的一個JavaScript對象&發送給我的PHP PDO來更新我的數據庫。我已將我的表單中的所有數據收集到一個JavaScript對象中。我使用json.stringify將對象轉換爲這種格式。 CONSOLE.LOG(updateString)生產:發送Javascript對象到PDO更新

[{"id":"20","type":"2","content":"3","name":"2","user":"1"}, 
    {"id":"21","type":"2","content":"4","name":"3","user":"1"}] 

我使用AJAX發送給我的PHP:

$.ajax({ 
    url: 'add.php', 
    data: { 
     updates: updateString 
    }, 
    type: 'POST', 
    datatype: 'application/json', 
    success: function (response) { 
     console.log(response); 
    }, 
    error: function (e) { 
     console.log(e); 
    } 
}); 

然後在我的PHP代碼,我解碼JSON &我試圖更新我的數據庫:

$db= new PDO ('mysql:dbname=xxxx;host=localhost', 'xxxx', 'xxxx'); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$updates = JSON_decode($_POST["updates"], true); 
try { 
    $sql = ("UPDATE tableName SET type=:type, content=:content, name=:name, user=:user WHERE id=:id"); 
    $stmt = $db->prepare($sql); 
    $stmt->bindParam(":id", $updates['id'], PDO::PARAM_STR); 
    $stmt->bindParam(":type", $updates['type'], PDO::PARAM_STR); 
    $stmt->bindParam(":content", $updates['content'], PDO::PARAM_STR); 
    $stmt->bindParam(":name", $updates['name'], PDO::PARAM_STR); 
    $stmt->bindParam(":user", $updates['user'], PDO::PARAM_STR);  
    $stmt->execute(); 
} 
catch(PDOException $e) { 
    echo "The changes could not be made.<br>".$e->getMessage(); 
} 

我沒有得到拋出,所以我不知道從哪裏開始的任何錯誤,但我的數據庫沒有更新。我知道我需要在我的PHP foreach循環,但我試圖讓單個數組先通過,然後再複雜化它。任何幫助表示讚賞。謝謝!

+0

請添加代碼:'的error_reporting(E_ALL);在開始時'。數據沒有被保存到數據庫中? –

+0

那麼你有什麼問題 - 爲我寫適當的代碼? –

+0

我的問題是你能告訴我我哪裏出錯了。 – heat222

回答

1

這裏只有一個,你應該有做得到這個工作變化不大:

try{ 
    for($i=0; $i<count($updates); $i++){ 
     $sql = ("UPDATE tableName SET type=:type, content=:content, name=:name, user=:user WHERE id=:id"); 
     $stmt = $db->prepare($sql); 
     $stmt->bindParam(":id", $updates[$i]['id'], PDO::PARAM_STR); 
     $stmt->bindParam(":type", $updates[$i]['type'], PDO::PARAM_STR); 
     $stmt->bindParam(":content", $updates['content'], PDO::PARAM_STR); 
     $stmt->bindParam(":name", $updates[$i]['name'], PDO::PARAM_STR); 
     $stmt->bindParam(":user", $updates[$i]['user'], PDO::PARAM_STR);  
     $stmt->execute(); 
    } 
} 
catch(PDOException $e) { 
    echo "The changes could not be made.<br>".$e->getMessage(); 
} 


原因:
作爲一個獨立的代碼,如果你試着運行以下,你」 LL看到JSON對象是如何在PHP接收:

$test = '[{"id":"20","type":"2","content":"3","name":"2","user":"1"}, 

      {"id":"21","type":"2","content":"4","name":"3","user":"1"}]'; 


$test_array = json_decode($test, true); 

for($i=0; $i<count($test_array); $i++){ 
    echo "<br> Id = ".$test_array[$i]['id']." | ".$test_array[$i]['content']; 
} 

產生將作爲輸出如下:

Id = 20 | 3 
Id = 21 | 4 



foreach版本相同的代碼將是:

try{ 
    $sql = ("UPDATE tableName SET type=:type, content=:content, name=:name, user=:user WHERE id=:id"); 
    $stmt = $db->prepare($sql); 
    $stmt->bindParam(":id", $id, PDO::PARAM_STR); 
    $stmt->bindParam(":type", $type, PDO::PARAM_STR); 
    $stmt->bindParam(":content", $content, PDO::PARAM_STR); 
    $stmt->bindParam(":name", $name, PDO::PARAM_STR); 
    $stmt->bindParam(":user", $user, PDO::PARAM_STR);  

    foreach($updates as $update_rec){ 
     $id  = $update_rec['id']; 
     $type = $update_rec['type']; 
     $content = $update_rec['content']; 
     $name = $update_rec['name']; 
     $user = $update_rec['user']; 
     $stmt->execute(); 
    } 
} 
catch(PDOException $e) { 
    echo "The changes could not be made.<br>".$e->getMessage(); 
} 
+0

'foreach'循環可能比for循環更容易管理。 – aynber

+0

@aynber謝謝。也增加了'foreach'版本。 –

+0

我認爲這可能是我需要的,但我仍然無法正常工作。在調試模式下,我可以通過函數一步一步執行到執行行。它顯示所有變量都在那裏,我從來沒有碰到過,所以它成功了。但是,它仍然沒有反映在我的數據庫中。當我在應用程序的其他部分發帖時,我確定連接。我檢查了我的本地主機也具有所有權限。 – heat222