1
I need to update a mysql table with data coming from a/two checkbox where checked is 2 and unchecked is 1.
It turns out that if you check the id6 of the col1 it will update the id4.
What will be wrong?
My mysql table looks like this:
id col1 col2
1 1 1
2 2 1
3 2 1
4 1 2
5 1 2
6 1 2
7 1 2
The way I set up the update:
I use a form as shown below:
<form id="form2" name="form2" method="post" action="<?php echo $PHP_SELF;?>">
my checkbox plus id:
<input name="id[]" id="id" type="text" value="<?php echo $row['id'];?>" />
<input name="col1[]" type="checkbox" id="col1[]" value="2" <?php if($row['col1']==2) echo 'checked="checked"';?> />
<input name="col2[]" type="checkbox" id="col2[]" value="2" <?php if($row['col2']==2) echo 'checked="checked"';?> />
My code on the page itself:
<?php
if(isset($_POST)){
//-->
$count = count($_POST['id']);
$i = 0;
//-->
while ($i < $count) {
$id = $_POST['id'][$i];
$col1= $_POST['col1'][$i];
//-->
$query = "UPDATE table SET col1= '$col1' WHERE id = '$id'";
mysqli_query($con,$query);
//-->
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=index.php">';
++$i;
}
}
?>
Not updating only the first record because of the meta refresh? Try to put it after the while
– Max Rogério
Tip: I convert checkboxes to json and save them in the bank as a string, it’s easier to recover and add more options without having to change the database
– Sveen