×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!
  • Students Click Here

*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

Jobs

FORMS

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

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

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><input name = "DLApp" type="checkbox"
    <?php echo (empty($post['DLApp'])) ? '' : ' checked="checked" ';?> value="1"/>
</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><input name = "DLPros" type="checkbox"  
    <?php echo (empty($post['DLPros'])) ? '' : ' checked="checked" ';?>value="1"/>
</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><input name = "Registered" type="checkbox"
    <?php echo (empty($post['Registered'])) ? '' : ' checked="checked" ';?> value="1"/>
</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)
{
 $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
                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',
                DLApp = $DLapp,
                DLPros = $DLPros,
                Registered = $Reg,

                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();
    }
    
}    

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