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);

So, arrays are on php side. Now, I need to update database this way:
- for each ids element find the corresponding row
- update the row (column inde) with value of parallel indexes element.

Something like this:

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();
}

Any help?

2
  • Why or what isn't working here? Commented Jan 13, 2017 at 22:09
  • @MeesKluivers, how to update database using arrays elements? Please notice ` ":id" => $ids-element,` and ":inde" => $indexes-element. Commented Jan 13, 2017 at 22:21

1 Answer 1

1

Can you even use hyphens in php variable names? Try:

    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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, I will test it later, pls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.