guddu guddu - 1 year ago 52
MySQL Question

how to update multiple ids with different value

My form have multiple checkboxes in it (each with the code):
<input type="checkbox" name="id[]" value="<? echo $row['id'] ?>">

when user select id = 2 , 3 and 9 and submit the value. In my update php i am using following code to get selected value

echo "Check box test<pre>" ;
echo "</pre>";
if(!empty($_POST['id'])) {
foreach($_POST['id'] as $check) {
echo $check."\n";

// update data in mysql database
$sql="UPDATE table SET display = '2' WHERE id IN ($check)";

i am always getting last selected id updated like 9

but i am not getting result as i wanted like

$sql="UPDATE table SET display = '2' WHERE id IN (2,3,9)";

. please Help what to do. i am very new to php.

Answer Source

You are doing nothing with $check, Make id like this

 if(!empty($_POST['id'])) {
   foreach($_POST['id'] as $id) {
      $ids[] = $id;
 $check = implode(",", $ids);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download