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
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";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.