HELP - insert a form 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

HELP - insert a form array into mysql

Post by timwhelan »

I have been trying to find the right way to do this...

I have a form where you can select one or many subjects...

Code: Select all

<tr>
        <td align="right" style="vertical-align:top;"><p>Subject Areas Taught</p></td>
        <td  style="vertical-align:top;">
        <input name="newsubmath" value="Math" type="checkbox">Math<br />
        <input name="newsubscience" value="Science" type="checkbox">Science<br />
        <input name="newsubsocial" value="SocialScience" type="checkbox">Social Science<br />
        <input name="newsublang" value="Languagearts" type="checkbox">Language Arts<br />
        <input name="newsubtech" value="Technology" type="checkbox">Technology<br />
        <input name="newsubspecial" value="Specialed" type="checkbox">Special Ed <br />
        <input name="newsubgifted" value="Gifted" type="checkbox">Gifted<br />
        <input name="newsubother" value="Other" type="checkbox">Other<br />
        </td>
    </tr>
I wanted to use a single cell but couldn't figure it out. The code above actually was a thought that I should push each one into its own cell. But that was giving me defined errors, which I haven't figure out. yet either.

How can I write the form and the php to insert into a mysql.

Any help is appreciated!!!
Thanks
Tim
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: HELP - insert a form array into mysql

Post by papa »

Code: Select all

 
<form method="post" action="submit.php">
<input name="options[]" value="Math" type="checkbox">Math<br />
<input name="options[]" value="Science" type="checkbox">Math<br />
 
etc.

And then loop thru your posted values on your submit page:

Code: Select all

 
foreach($_POST as $value)
{
 print_r($value)
}
timwhelan
Forum Newbie
Posts: 8
Joined: Mon Sep 18, 2006 11:34 am

Re: HELP - insert a form array into mysql

Post by timwhelan »

okay one more dumb question, where in the code below do I place it?

Code: Select all

   <?php
else:
    // Process signup submission
    dbConnect('db');
 
    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.');
    }
 
    /*Concat Checkbox Values into One string separated by commas for Select*/
    foreach ($newethnicity as $value){
    $search=$search.",".$value;
    }
    /* Remove Leading comma*/
    $searchit=substr($search,1);
 
    $sql = "INSERT INTO user SET
             userid = '$_POST[newpersonalemail]',
              password = '$_POST[newpass]',
             firstname = '$_POST[newfirstname]',
              middlename = '$_POST[newmiddlename]',
              lastname = '$_POST[newlastname]',
              ethnicity = $search
              ";
      
    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());
             
I am a little confused and trying to learn.
thanks
Post Reply