I am trying to update some rows in mysql based on selected checkbox,but my query is updating all the rows in the table. Please check below code:
if (!empty($_POST['checkbox']))
{
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['checkbox'] as $selected) {
$sql1="UPDATE Items SET Status='1' WHERE Item_Id='$selected'";
$result1=mysql_query($sql1);
if (!$result1)
{
echo "Error While updating";
}
else
{
header("Refresh: 2;url=Main_Menu.php");
mysql_close($conn);
echo "<p>Todays Menu Updated <a href='Main_Menu.php>Click here</a> if you are not redirected automatically in 2 seconds<br /></p>";
}
}
Here if (!empty($_POST['checkbox']))
you should use the name of your checkbox.
For instance, if your html is like below:
<input type="checkbox" name="checkbox_name" />
You should write the if statement as below:
if (!empty($_POST['checkbox_name']))
The same holds for the foreach
statement and the use of this in your sql statement.