0

An array containing the primary key values of a mysql table will be passed through an ajax call to a php page and from the php page, rows with the primary keys in the array will be updated

js snippet:

<script type="text/javascript">

$(document).ready(function(){


    var my_array=[1,2.,4,5,6];

    $.post("array_post.php",{

        my_array:my_array

        },function(response){

            alert(response);
            }

    );



    });


</script>

'id' is the primary key. array_post.php has :

$my_array=$_POST['my_array'];

$sql="UPDATE my_table SET my_col=1 WHERE id IN ($my_array)";

Bu that shows error.

I need the solution.

1
  • something like : Sorry the update query could not be executed into table post_comment notification</br>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 'Array' at line 1 Commented Feb 9, 2012 at 10:53

2 Answers 2

1

You need to transform your array to a string:

$sql="UPDATE my_table SET my_col=1 WHERE id IN (" . implode(',', $my_array) . ")";

And, you should seriously validate your input, not just use $_POST

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

2 Comments

so, 'IN' key word searches in a comma separated string , not in an array, right?
Yeah, well, you just can't use an PHP array in an SQL String. It need to look like IN (1,2,3).
1

your array must be comma separated so use implode(",",$my_array) instead of using just $my_array

1 Comment

implode returns a string, not an array

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.