Jump to content

Checkbox and SQL


Gustav

Recommended Posts

Hi guys,

 

What im stuck with at the moment is how to update the SQL database with the new information acquired from a set of check boxes.

 

What i´ve done so far is to create the checkboxes and got the information from a table which checkboxes is needed and pre check it if it is already enabled. i have two rows in the database 1. Module_Name (name of a module) 2. is_enabled (is the module enabled 1 or 0).

 

The code i have so far is :

        
<FORM action="modules_checked.php" method="post">
        <?PHP
	$array_row = mysql_query("SELECT * FROM check_modules");
	while($row = mysql_fetch_array($array_row)){
		$module_name = $row["Module_name"];
		$array_enabled = mysql_query("SELECT is_enabled FROM check_modules where Module_name='$module_name'");
		$enabled = mysql_fetch_array($array_enabled);
		$db_box1 = $row["is_enabled"];
		if($db_box1 == 1)  {$set_checked = "CHECKED";}
			else{$set_checked  =  "";} 
		print "<input type=\"checkbox\"  NAME=\"module[]\"  VALUE=\"$module_name\" $set_checked/> $module_name <br>";
		$set_checked = ""; 
	} 
	?> 
<br>
<input type="submit" name="submit" value="Submit" />
</FORM>

 

This part works flawlessly :). However i have no clue on how to manage it when someone checks or unchecks and presses submit i can send that to the SQL.

 

With pre thanks,

 

Gustav

Link to comment
https://forums.phpfreaks.com/topic/73956-checkbox-and-sql/
Share on other sites

i typically store checkbox values in MySQL as 0 or 1, that is field of type TINYINT, UNSIGNED. since checkboxes offer multiple options, i give each checkbox a separate name and i set all checkbox values to 1. i do not group checkboxes using an array; i only do that for radio buttons. (so i wouldn't use multiple checkboxes called "module[]"). to get the value of a checkbox for SQL, i use:

 

$checkval = ($_POST['module'] == 1)?1:0;

$sql = "UPDATE some_table SET module = '$checkval' WHERE id = '$someid'";
// etc...

 

Link to comment
https://forums.phpfreaks.com/topic/73956-checkbox-and-sql/#findComment-373266
Share on other sites

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.