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