Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Checkboxes - how to use them with Mysql dbs by jedel
Posted: 4 Jan 09

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: After update property on a form.

So, to start. You have a form...


<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" />

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


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


<input name = "DLPros" type="checkbox"  
    <?php echo (empty($post['DLPros'])) ? '' : ' checked="checked" ';?>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


<?php echo $params['users']?>
<legend> Add / Edit Students</legend>
<table align="center" width="80%" cellpadding="2" border ="0">
    <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>
    <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>
    <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>
  <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>
  <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>
  <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>
  <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>
    <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>
    <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>
  <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>
  <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>
    <td height="23" bgcolor="#DDDDDD">Downloads</td>
    <td>Student ID: <?php echo $post['StudentID'];?></td>
    <td align="right" bgcolor="#DDDDDD">Application</td>
    <td><input name = "DLApp" type="checkbox"
    <?php echo (empty($post['DLApp'])) ? '' : ' checked="checked" ';?> value="1"/>
    <td width="11%" bgcolor="#DDDDDD">Last update</td>
    <td width="41%"><?php echo $post['LastUD'];?></td>

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

    <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>
  <td align="right" bgcolor="#DDDDDD">Student Has Registered</td>
  <td><input name = "Registered" type="checkbox"
    <?php echo (empty($post['Registered'])) ? '' : ' checked="checked" ';?> value="1"/>
  <td bgcolor="#DDDDDD">&nbsp;</td>

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

this is the SQL that updates the database


function update_student($params)
 $DLapp = isset($_POST['DLApp'])?1:0 ;
 $DLPros = isset($_POST['DLPros'])?1:0;
 $Reg = isset($_POST['Registered'])?1:0;

    $connection = db_connect();
    $query = sprintf("update students
                Surname = '%s',
                FirstName = '%s',
                Email = '%s',
                Street = '%s',
                Suburb = '%s',
                City = '%s',
                PCode = '%s',
                State = '%s',
                Comments = '%s',
                Phone = '%s',
                Login = '%s',
                Pwd = '%s',
                DLApp = $DLapp,
                DLPros = $DLPros,
                Registered = $Reg,

                UDBy = '%s'
                StudentID = '%s'

    $result = mysql_query($query);
        return false;
        //echo mysql_error();
        return true;
        //echo mysql_error();

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close