Jump to content

Checkbox and MySQL help


whitemoss

Recommended Posts

Hi all,

 

I would like to write a code to immediately update the database, once a checkbox has been checked or unchecked. I already wrote a code for updating purposes but it didnt work as expected. It only brings the same value or id of checkbox even though I select other checkbox or select more than 1 checkbox. below is the code:

 

<? 
$i = 1;
$j = 1;
while ($row = mysql_fetch_array($result)) 
{
    //while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $bil 	= $i; //+ ($PageaNo-1)*$PageSize;
    $id		= $row['RecordID'];
    $date	= $row['Date'];
    $event	= $row['Event']; 
    ?>
       <tr bgcolor="#CCCCCC">
       <td align="center" width="50"><?php echo $bil; ?></td>
       <td align="center" width="50"><? echo "<input name='checkbox[$id]' id='$id' type='checkbox' value='$id'>"?></td>
       <td align="center" width="250"><?php echo $event; ?></td>

<?php $i++; $j++; } ?>
....
.... some codes here
....

<td align="center">
<form name="form4" method="post" action="newPublicHoliday.php">
<input name="Add" type="submit" id="Add" value="Add Event">
</form>
</td>

<? if ($_POST['Submit'])
{
	//$checkbox	= $_POST["checkbox"];
	$checkbox = $_POST['checkbox'];
	$id	= $_POST['id'];
	echo $checkbox;
	echo $id;
	//foreach ($checkbox as $key => $value )
	//{	//echo $id[$key]." ".$value;
		$update = "Update Holiday set Status=1 where RecordID=$id";
		mysql_query($update);
		echo $update;
	//}

} ?>

Link to comment
https://forums.phpfreaks.com/topic/40464-checkbox-and-mysql-help/
Share on other sites

see this sample:

 

	if(isset($_POST['leaves'])) {
	foreach ($_POST['leaves'] as $value) {
		$sql="update leave_apps set archive=1 where leave_apps.id='$value'";
		$c_leaves->query($sql);
	}
}


	while ($row=$c_leaves->fetch_array($res)) {
		echo "<tr class='alt1'>";
		echo "<td class='data' width=1%><input type=checkbox name='leaves[]' value='$row[leave_id]' /></td>";
		echo "</tr";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.