Code to insert checkbox array into mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timwhelan
Forum Newbie
Posts: 8
Joined: Mon Sep 18, 2006 11:34 am

Code to insert checkbox array into mysql

Post by timwhelan »

Okay I have spent too long trying to figure this out and am lost and tired.

I have a form and in that form I have a field - Subjects Taught (1, 2, 3, 4) as check boxes

When I submit the form I can get all fields but this one set up. The code I am using to insert is below

Code: Select all

 
    // Process signup submission
    dbConnect('database);
 
    if ($_POST['newpersonalemail']=='' or $_POST['newfirstname']==''
      or $_POST['newlastname']=='') {
        error('One or more required fields were left blank.\\n'.
              'Please fill them in and try again.');
    }
    
    // Check for existing user with the new id
    $sql = "SELECT COUNT(*) FROM user WHERE userid = '$_POST[newpersonalemail]'";
    $result = mysql_query($sql);
    if (!$result) { 
        error('A database error occurred in processing your '.
              'submission.\\nIf this error persists, please '.
              'email.');
    }
    if (mysql_result($result,0,0)>0) {
        error('A user already exists with your chosen newpersonalemail.\\n'.
              'Please try another.');
    }
 
$sql = "INSERT INTO user SET
userid = '$_POST[newpersonalemail]',
password = '$_POST[newpass]',
firstname = '$_POST[newfirstname]',
middlename = '$_POST[newmiddlename]',
lastname = '$_POST[newlastname]',
subtaught = '$_POST[subtaught]'
";
              
    if (!mysql_query($sql))
        error('A database error occurred in processing your '.
              'submission.\\nIf this error persists, please '.
              'contact you@example.com.\\n' . mysql_error());
      
 
So I have the form set up as such:

Code: Select all

 
...
   <tr>
        <td align="right" style="vertical-align:top;"><p>Subjects Taught</p></td>
        <td  style="vertical-align:top;">
    <input name="subtaught[]" value="Math" type="checkbox">Math<br />
        <input name="subtaught[]" value="Science" type="checkbox">Science<br />
        <input name="subtaught[]" value="SocialScience" type="checkbox">Social Science<br />
        <input name="subtaught[]" value="LanguageArts" type="checkbox">Language Arts<br />
        <input name="subtaught[]" value="Technology" type="checkbox">Technology<br />
        </td>
    </tr> 
...
 
How do I write the insert into what I have above?

Thank you!!!!
Post Reply