Help! I'm new to php so stick with me! I have a page that loops through my database and prints student names with a checkbox alongside each name. When you select a checkbox and hit submit, it saves a value of 1 to the db.
This is working fine but what i'm trying to so is that if the checkbox is already populated and the user unchecks it, i want it ti save the db back to 0.
I know you can't pass values of an unchecked checkbox. So when the user submits the form, i'm trying to write everything in the db to 0, before it starts looping through the checkboxes and setting the appropiate values to 1, where they are checked. That way whatever has been unchecked will go back to 0.
Here is some of the php code in the form displaying the checkboxes. The LogID is the PK in the table where the column name 'Value' is either 0 or 1.
<td><input type=checkbox name='box[]' value=\"".$row2['LogID']."\" CHECKED></td>
There is a submit button at the end of this form. Here's the code for when this button is clicked.
<?php
include ("connect.php");
if ($_POST['Save'])
{
if(isset($_POST['box'])){
$box = $_POST['box'];
while (list ($key,$val) = @each ($box)) {
$sqlupdate="UPDATE tblReportLog SET `Value` = '1' WHERE LogID='$val'";
$resdel=mysql_query($sqlupdate);
}
}
else{
echo "No records selected.";
}
}
?>
Is it possible to put a query somewhere in here wher i can the checkbox value to 0?
Or would an alternative be to put a 'hidden' value in the first page and loop through the values that are recorded by this for every entry in the table with LogID as the PK.
Sorry if this is confusing but any help on this would be great!!
Thank you!
Ned help with checkboxes (php/mysql)
Moderator: General Moderators
-
ciaranholland
- Forum Newbie
- Posts: 6
- Joined: Sat Feb 20, 2010 6:50 pm
Re: Ned help with checkboxes (php/mysql)
Why don't use list(option menu) instead checkboxes.
List can have more values. For example 1 - active, 2 -insctive.
List can have more values. For example 1 - active, 2 -insctive.
Re: Ned help with checkboxes (php/mysql)
You could always just set them all to zero first, then reset the desired records to a value of 1... insert something like this before your $sqlupdate definition:
Code: Select all
$sqlupdate="UPDATE tblReportLog SET `Value` = '0'";
mysql_query($sqlupdate);