2017-01-13 6 views
1
<div data-id=1448 class='item folder'>lorem ipsum</div> 
<div data-id=1393 class='item folder'>lorem ipsum</div> 
<div data-id=1441 class='item folder'>lorem ipsum</div> 
<div data-id=1442 class='item folder'>lorem ipsum</div> 

JSだから配列を使ってデータベースを更新するには?

$('#mpsave').click(function(){ 
    var ids = []; 
    var indexes = []; 
    $('.folder').each(function(){ 
     ids.push($(this).data('id')); 
     indexes.push($(this).index()); 
    }); 
    console.log(ids); // works well 
    console.log(indexes); // works well 

$.ajax({ 
    url: 'target.php', 
    type: 'post', 
    data: {'ids' : ids, 'indexes' : indexes}, 
    success: function(data){ 
     console.log(data); // works well 
    } 
}); 
}); 

target.php

extract($_POST); 

print_r($ids); 
print_r($indexes); 

、アレイは、PHP側にあります。
- 各ids要素に対して、対応する行を見つけます。
- 値がパラレルの値indexesのロー(列inde)を更新します。

このような何か:

try { 
$stmt = $db->prepare("UPDATE folders SET inde = :inde WHERE id = :id"); 
$stmt->execute(array(
    ":id" => $ids-element, 
    ":inde" => $indexes-element 
)); 
} 
catch(PDOException $e) { 
echo $e->getMessage(); 
} 

任意のヘルプ?

+0

なぜかここで何を動作していませんか? –

+0

@MeesKluivers、配列要素を使ってデータベースを更新する方法は? '' id => $ ids-element、 '':inde "=> $ indexes-element'に注意してください。 – bonaca

答えて

1

php変数名にハイフンを使用できますか?試してみてください:

for ($i = 0; $i <= count($ids); $i++){ 
      try { 
       $stmt = $db->prepare("UPDATE folders SET inde = ? WHERE id = ?"); 
       $stmt->bind_param("ii",$ids[i],$indexes[i]); 
       $stmt->execute(); 
      } 
      catch(PDOException $e) { 
       echo $e->getMessage(); 
      } 
    } 

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php http://php.net/manual/en/mysqli-stmt.bind-param.php

+0

ありがとう、私は後でそれをテストします、pls。 – bonaca

関連する問題