|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 ![]() |
|
#3
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > what about checkboxes and mysql? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|