2017-01-23 92 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