Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox update

Status
Not open for further replies.

Brianfree

Programmer
Feb 6, 2008
220
GB
Hi, i want to be able to have a check box for each row called required. By default all check boxes are ticked. If a user deselects multiple check boxes i want to run an update query to change the value for each selected row to "not required"

Hope this makes sense, please can anyone help!!

Many thanks,

Brian
 
I have found some code which i think i can modify to suit my needs...


Code:
<?php
$chk=",";
        if (isset($_POST["SaveChanges"]))
	   {
                $Supply = "";
 
           
		   for ($i=0; $i < count($_POST['Supply']); $i++) {
                        $Supply = $Supply . $_POST['Supply'][$i] . " "; 
				    $chk .= $_POST['Supply'][$i] . "," ;
                }
			 $_SESSION['Supply']=$Supply;	
                $updateSQL = sprintf("UPDATE Job_Details SET Supply= 'No' WHERE id=%s", $Supply);
				echo $updateSQL . "<br>";

        }
	   elseif( isset($_SESSION['Supply']) )
	   {
	   	$chk=$_SESSION['Supply'];
	   }
	   ?>
 
<form name="updateForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 
<input type="checkbox" name="Supply[]" value="1"></input>Starter
<input type="checkbox" name="Supply[]" value="2"></input>Alternator
 
<input type="submit" name="SaveChanges" value="Save Changes">
 
</form>

How can this update multiple rows in the database?

Please can someone help me???

Many thanks,

Brian
 
I think this will work, please can someone advise??

Code:
<?php
$chk=",";
        if (isset($_POST["SaveChanges"]))
	   {
                $Supply = "";
 
           
		   for ($i=0; $i < count($_POST['Supply']); $i++) {
                $Supply = $Supply . $_POST['Supply'][$i] . " "; 
				$chk .= $_POST['Supply'][$i] . "," ;
		  	    $_SESSION['Supply']=$Supply;	
                $updateSQL = sprintf("UPDATE Job_Details SET Supply= 'No' WHERE id=%s", $_POST['Supply'][$i]);
				echo $updateSQL . "<br>";
	       }
        }
	   elseif( isset($_SESSION['Supply']) )
	   {
	   	$chk=$_SESSION['Supply'];
	   }
	   ?>

Many thanks,

Brian
 
Ok - i have kind of got it to work but only one way.. If the check box is selected then i want supply to equal "Yes", else supply to equal "No". How can i get my code to toggle what is written to the database?

This is my php code..

Code:
<?php
$chk=",";
        if (isset($_POST["SaveChanges"]))
	   {
                $Supply = "";
 
           
		   for ($i=0; $i < count($_POST['Supply']); $i++) {
                $Supply = $Supply . $_POST['Supply'][$i] . " "; 
				$chk .= $_POST['Supply'][$i] . "," ;
		  	    $_SESSION['Supply']=$Supply;
					mysql_select_db($database_yardbuying, $yardbuying);
					$updateSQL = sprintf("UPDATE Job_Details SET Supply = 'Yes' WHERE id=%s", $_POST['Supply'][$i]);
					$RecordsetUpdate1 = mysql_query($updateSQL, $yardbuying) or die(mysql_error());
				echo $updateSQL . "<br>";
	       }
        }
	   elseif( isset($_SESSION['Supply']) )
	   {
	   	$chk=$_SESSION['Supply'];
	   }
?>

This is by checkbox...

Code:
<input type="checkbox" class="button" <?php if(strstr($chk,$Unique_ID)){echo "checked='checked'";} ?> name="Supply[]" id="<?php echo $Unique_ID?>" value="<?php echo $Unique_ID?>"/></input>


Many thanks,

Brian
 
Im a little stuck trying to figure this one out! Please can someone show me where i am going wrong??

Many thanks,

Brian
 
Basically, you need to check for the existence of your checkbox variables, as unchecked checkboxes will not be passed over and PHP will have no variable for it.

Taking your example of 2 checkboxes, if one was unchecked instead of getting two $_POST values for your loop you'll only get one.

Now your loop will update your field over and over with a value from the $_POST['supply'] array, but it will overwrite it with the following value.

Now you seem to have two checkboxes, but yet you only update one field. how do you determine which value is the one it corresponds to: Alternator or Starter?




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi thanks for replying. I wasn't quite sure where to start but found some code which resembled slightly what i was trying to do so i used this as a starting point... The code above might not be correct as i have been messing it around in order to get somthing to work.

This is what i am trying to do...

I have some product data in a table, there is a required field against each product. I simply want to display the products on the screen with a check box next to each one, if the check box is ticked then the product is required, if it is not ticked then it is not required. Then there is a button which updates the record(s).

If you could show me an example of how to do this or point me in the right direction; it would be most appreciated.

Many thanks,

Brian
 
This is a somewhat dirty example, but you should get the idea:

This is all in the same page, so the display and the updates are separated by conditional statements.

Code:
$conn=mysql_connect("localhost","root","") or die(mysql_error()); [green]\\db connection[/green]

$db=mysql_select_db("test") or die(mysql_error()); [green]\\select DB[/green]

if(isset($_POST['update'])){  [green]\\check if fomr was submitted[/green]

$update=array(); [green]\\create array to hold required and un-required values[/green]

if(isset($_POST['required']['req_1'])){ [green]\\check which checkboxes were submitted, and and which were not, and create the array with the appropriate values for the update[/green]
$update['req1']='TRUE';
}
else{
$update['req1']='FALSE';
}
if(isset($_POST['required']['req_2'])){
$update['req2']='TRUE';
}
else{
$update['req2']='FALSE';
}
if(isset($_POST['required']['req_3'])){
$update['req3']='TRUE';
}
else{
$update['req3']='FALSE';
}
if(isset($_POST['required']['req_4'])){
$update['req4']='TRUE';
}
else{
$update['req4']='FALSE';
}

[green]\\you'll need to check for as many checkboxes as you have[/green]
$query2="UPDATE required SET req_1=$update[req1], req_2=$update[req2], req_3=$update[req3], req_4=$update[req4] WHERE id_user=2";
[green]\\build update query[/green]

if($res=mysql_query($query2)){
echo "Update Successfull";
} [green]\\Execute query[/green]

}

[green]\\ From here, we build the form to show the chekced and unchecked values from the DB[/green]
$query="Select * FROM required WHERE id_user=2";

$results=mysql_query($query) or die(mysql_error());
echo '<form action="word.php" method="POST">';
$var2="Test";
$i=0;
while($row=mysql_fetch_array($results)){

$values['ch1']=box_checker($row['req_1']);
$values['ch2']=box_checker($row['req_2']);
$values['ch3']=box_checker($row['req_3']);
$values['ch4']=box_checker($row['req_4']);
echo<<<EOE
User: $row[id_user] ---
<input type="checkbox" value="Required" name="required[req_1]" $values[ch1]>
<input type="checkbox" value="Required" name="required[req_2]" $values[ch2]>
<input type="checkbox" value="Required" name="required[req_3]" $values[ch3]>
<input type="checkbox" value="Required" name="required[req_4]" $values[ch4]>
<br>

EOE;
$i++;
}


echo "<input type=submit value='Update Requirements' name='update'></form>";



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for replying! will have a play with this code over the weekend!

Many thanks,

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top