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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkboxes - how to use them with Mysql dbs

FORMS

Checkboxes - how to use them with Mysql dbs

by  jedel  Posted    (Edited  )
After a lot of tearing of hair and gnashing of teeth, jpadie and tsuji helped me with the proper use of checkboxes in a PHP coded form itegrated with a MySQL database. If you woul like to follow the thread, it's located at thread181-27567.

So, to start. You have a form...
Code:
<form id="form1" name="form1" method="post" ction="somepage.php">
<input name = "Suburb" type="text" size="30" value="somevalue from db" />

<input name = "DLApp" type="checkbox" value="1"/>

<?php include('_form.php');?>
<input type="submit" value="update" />
</form>

The associated field for the checkbox should be set as a Boolean with an empty Default value (although the latter is not too important.)

The value of a checkbox does very little when it comes to providing the right information to a database field. To get the right data into the database your SQL should look something like this

Code:
<?php
function update_data();

$DLapp = isset($_POST['DLApp'])?1:0;

$query = "update tablename set DLApp = $DLApp where recordId = ID"

?>

The name of the checkbox should be "DLApp" in this case so that it mathces the code above.

If you want to update the data, you need to make sure that the checkbox can check the value of the database and then check the box if the value is 1. This is done like so..

Code:
<input name = "DLPros" type="checkbox"  
	[red]<?php echo (empty($post['DLPros'])) ? '' : ' checked="checked" ';?>[/red]value="1"/>

and that's about it. Below is the an example of my project for you to look over. It may appear as though some code is missing, but I assure you it is not. Its an MVC structured site.

This is the form
Code:
<fieldset>
<?php echo $params['users']?>
<legend> Add / Edit Students</legend>
<table align="center" width="80%" cellpadding="2" border ="0">
<tr>
	<td width="26%" align="right" bgcolor="#DDDDDD">Surname</td>
	<td width="22%"><input name = "student[Surname]" type="text" size="30" value="<?php echo element_value($errors['Surname'], $post['Surname'],$params['student']['Surname']);?>" /></td>
	<td colspan="2" bgcolor="#DDDDDD">Comments</td>
  </tr>
<tr>
	<td align="right" bgcolor="#DDDDDD">First name</td>
	<td><input name = "student[FirstName]" type="text" size="30" value="<?php echo element_value($errors['FirstName'], $post['FirstName'],$params['student']['FirstName']);?>" /></td>
	<td colspan="2" rowspan="11" align="left" valign="top"><textarea name = student[Comments] cols="50" rows="15" wrap="ON" hieght = "10"><?php echo element_value($errors['Comments'], $post['Comments'],$params['student']['Comments']);?></textarea></td>
  </tr>
<tr>
	<td align="right" bgcolor="#DDDDDD">Street</td>
	<td><input name = "student[Street]" type="text" size="30" value="<?php echo element_value($errors['Street'], $post['Street'],$params['student']['Street']);?>" /></td>
  </tr>
<tr>
  <td align="right" bgcolor="#DDDDDD">Suburb</td>
  <td><input name = "student[Suburb]" type="text" size="30" value="<?php echo element_value($errors['Suburb'], $post['Suburb'],$params['student']['Suburb']);?>" /></td>
</tr>
<tr>
  <td align="right" bgcolor="#DDDDDD">City</td>
  <td><input name = "student[City]" type="text" size="30" value="<?php echo element_value($errors['City'], $post['City'],$params['student']['City']);?>" /></td>
</tr>
<tr>
  <td align="right" bgcolor="#DDDDDD">State</td>
  <td><input name = "student[State]" type="text" size="30" value="<?php echo element_value($errors['State'], $post['State'],$params['student']['State']);?>" /></td>
</tr>
<tr>
  <td align="right" bgcolor="#DDDDDD">Postcode</td>
  <td><input name = "student[PCode]" type="text" size="30" value="<?php echo element_value($errors['PCode'], $post['PCode'],$params['student']['PCode']);?>" /></td>
</tr>
<tr>
	<td align="right" bgcolor="#DDDDDD">Email</td>
	<td><input name = "student[Email]" type="text" size="30" value="<?php echo element_value($errors['Email'], $post['Email'],$params['student']['Email']);?>" /></td>
  </tr>
<tr>
	<td align="right" bgcolor="#DDDDDD">Phone</td>
	<td><input name = "student[Phone]" type="text" size="30" value="<?php echo element_value($errors['Phone'], $post['Phone'],$params['student']['Phone']);?>" /></td>
  </tr>
<tr>
  <td align="right" bgcolor="#DDDDDD">Login Name</td>
  <td><input name = "student[Login]" type="text" size="30" value="<?php echo element_value($errors['Login'], $post['Login'],$params['student']['Login']);?>" /></td>
  </tr>
<tr>
  <td align="right" bgcolor="#DDDDDD">Password</td>
  <td><input name = "student[Pwd]" type="text" size="30" value="<?php echo element_value($errors['Pwd'], $post['Pwd'],$params['student']['Pwd']);?>" /></td>
  </tr>
<tr>
	<td height="23" bgcolor="#DDDDDD">Downloads</td>
	<td>Student ID: <?php echo $post['StudentID'];?></td>
  </tr>
<tr>
	<td align="right" bgcolor="#DDDDDD">Application</td>
	<td>[red]<input name = "DLApp" type="checkbox" 
	<?php echo (empty($post['DLApp'])) ? '' : ' checked="checked" ';?> value="1"/>[/red]</td>
	<td width="11%" bgcolor="#DDDDDD">Last update</td>
	<td width="41%"><?php echo $post['LastUD'];?></td>
</tr>
<tr>

	<td align="right" bgcolor="#DDDDDD">Prospectus</td>
	<td>[red]<input name = "DLPros" type="checkbox"  
	<?php echo (empty($post['DLPros'])) ? '' : ' checked="checked" ';?>value="1"/>[/red]</td>

	<td bgcolor="#DDDDDD">By</td>
	<td><input name = "student[UDBy]" type="text" size="30" value="<?php echo current_user('Firstname').' '.current_user('Surname');?>" readonly/></td>
</tr>
<tr>
  <td align="right" bgcolor="#DDDDDD">Student Has Registered</td>
  <td>[red]<input name = "Registered" type="checkbox" 
	<?php echo (empty($post['Registered'])) ? '' : ' checked="checked" ';?> value="1"/>[/red]</td>
  <td bgcolor="#DDDDDD">&nbsp;</td>
  <td>&nbsp;</td>
</tr>
</table>

<input type="submit" value="update" />
</fieldset>

this is the SQL that updates the database
Code:
function update_student($params)
{
 [red]$DLapp = isset($_POST['DLApp'])?1:0 ;
 $DLPros = isset($_POST['DLPros'])?1:0;
 $Reg = isset($_POST['Registered'])?1:0;[/red]
   
	$connection = db_connect();
	$query = sprintf("update students
				set
				Surname = '%s',
				FirstName = '%s',
				Email = '%s',
				Street = '%s',
				Suburb = '%s',
				City = '%s',
				PCode = '%s',
				State = '%s',
				Comments = '%s',
				Phone = '%s',
				Login = '%s',
				Pwd = '%s',
				[red]DLApp = $DLapp,
				DLPros = $DLPros,
				Registered = $Reg,[/red]
				UDBy = '%s'
				
				where
				StudentID = '%s'
				",
				mysql_real_escape_string($params['Surname']),
				mysql_real_escape_string($params['FirstName']),
				mysql_real_escape_string($params['Email']),
				mysql_real_escape_string($params['Street']),
				mysql_real_escape_string($params['Suburb']),
				mysql_real_escape_string($params['City']),
				mysql_real_escape_string($params['PCode']),
				mysql_real_escape_string($params['State']),
				mysql_real_escape_string($params['Comments']),
				mysql_real_escape_string($params['Phone']),
				mysql_real_escape_string($params['Login']),
				mysql_real_escape_string($params['Pwd']),
				mysql_real_escape_string($params['UDBy']),
				mysql_real_escape_string($params['StudentID'])				
				);

	$result = mysql_query($query);
	
	if(!$result){
		
		return false;
		//echo mysql_error();
		
	}else{
		
		return true;
		//echo mysql_error();
	}
	
}
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top