#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    4
    Rep Power
    0
    This question is similar to one posted previously by icewind0. That person asked about storing responses from a select element on a form. The response to that was to create a table with the limited responses, etc. What about check boxes? I would imagine you could create a table to store all the possible answers, but how would you associate a variable number of answers with the user who answered them? For example, on a form that says: check all the sports you like, there could be 12 sports listed. Each user who fills out the form could pick 0, 1, or up to 12. If you have a table with the users data in it, what is the right/best way to store this type of input?

    Thanks for any help.

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    London
    Posts
    46
    Rep Power
    15
    Create a table in your db called sports for example:

    CREATE TABLE sports (
    like SET('football', 'tennis','rugby')
    );

    This field will now only take these three sporting values.

    Next here's some code to input into the table

    ********************* HTML ******************

    <?
    print "<HTML>n";
    print "<HEAD>n";
    print "</HEAD>n";

    print "<BODY>n";
    print("Choose a sport that you like<BR>n");

    print("<FORM ACTION="sportphp.php3">n");


    print("<INPUT TYPE="checkbox" NAME="sport[]" VALUE="football">Football");

    print("<INPUT TYPE="checkbox" NAME="sport[]" VALUE="tennis">Tennis");

    print("<INPUT TYPE="checkbox" NAME="sport[]" VALUE="rugby">Rugby");


    print("<INPUT TYPE="submit">n");
    print("</FORM>n");
    print("</BODY>n");
    print("</HTML>n");
    ?>

    On submit this will create an array '$sport[]' with the values of the 3 sports.

    ****************** php ********************

    <?
    if(isset($sports))
    {
    $mylink = mysql_connect('localhost','user','password');


    $query = "INSERT INTO sports(like) VALUES('$sports[0]','$sports[1]','$sports[2]')";

    $result = mysql_db_query('dbname',$query, $mylink) or die("Could not execute query");
    }
    else
    {
    print("No items were selected!");
    }
    ?>

    This should work although I just typed it now and haven't checked it for errors.

    Kelvin
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    4
    Rep Power
    0
    Thanks for the comprehensive reply, but that doesn't work. I get an error that says column count does not equal value count when I try the insert. Is it necessary to build a comma separated list of the values then insert that?

    Thanks.

Similar Threads

  1. How Do I Delete Selected Rows From MySQL Table?
    By Volitics in forum PHP Development
    Replies: 8
    Last Post: June 17th, 2003, 12:48 AM
  2. checkboxes - implode
    By homenetguy in forum PHP Development
    Replies: 4
    Last Post: June 9th, 2002, 09:19 PM
  3. updating DB with 2 different sets of dynamic checkboxes
    By scrtagt69 in forum PHP Development
    Replies: 8
    Last Post: May 16th, 2002, 10:35 AM
  4. Insert to MySQL with Checkboxes.
    By Hagan.B in forum PHP Development
    Replies: 4
    Last Post: September 28th, 2001, 06:23 AM
  5. checkboxes from a Mysql DB
    By tigg in forum PHP Development
    Replies: 3
    Last Post: August 11th, 2000, 02:25 PM

IMN logo majestic logo threadwatch logo seochat tools logo