Hello!
I have a script to update multiple rows in database but it update all and I need to only values in checked rows be updated. What should I change to update only checked rows?
<?php
include 'connect_db.php';
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
if($_POST['Submit'])
{
foreach($_POST['id'] as $id)
{
$sql1="UPDATE ".$tbl_name." SET status='".$_POST["status".$id]."', name='".$_POST["name".$id]."' WHERE id='".$id."'";
$result1=mysql_query($sql1);
}
if($result1){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=update_multiple.php\">";
}
}
else
{
?>
<strong>Update multiple rows in mysql</strong><br>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><strong>Id</strong></td>
<td><strong>Status</strong></td>
<td><strong>Name</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result))
{
?>
<tr>
<td><input type="hidden" name="id[]" value="<?php echo $rows['id']; ?>" /><?php echo $rows['id']; ?></td>
<td ><input name="status<?php echo $rows['id']; ?>" type="checkbox" id="status" value="1"
<?php if ($rows['status'] ==1) { echo "checked";} else {} ?>
></td>
<td><input name="name<?php echo $rows['id']; ?>" type="text" id="name" value="<?php echo $rows['name']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
}
mysql_close();
?>