KingPin's Forum
 
K.P.s.N. Register vbBux / KPs Mall Bugs Blogs FAQ Search Today's Posts Mark Forums Read Donate
Go Back   KingPin's Forum > KP's Network Forum > RSS News
Reload this Page Help submitting checkboxes into a mysql database?
 


RSS News This is a discussion on Help submitting checkboxes into a mysql database? in the RSS News;
Description: I'm new to PHP/SQL so help please I'm trying to create a form that will allow users to select certain ...

Reply
 
LinkBack Thread Tools
Help submitting checkboxes into a mysql database?
(#1 (permalink))
Old
mpeters411 is Offline
Private
Points: 151, Level: 2 Points: 151, Level: 2 Points: 151, Level: 2
Activity: 0% Activity: 0% Activity: 0%
mpeters411 is an unknown quantity at this point
 
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jan 2008
Help submitting checkboxes into a mysql database? - January 1st, 2008

I'm new to PHP/SQL so help please

I'm trying to create a form that will allow users to select certain foods, using checkboxes, and then submit that information to the table in mysql.

<form action = "choice2.php" method = "post">
<table border = "0">
<tr>
<td><input type = "checkbox" name= food[] value="Chicken
Parmesean">Chicken Parmesean
</td>
</tr>
<tr>
<td><input type="checkbox" name = food[] value="Fried Plantains"> Fried Plantains</td>
</tr>
</form>

<?

$food = $_POST['food']
$query = sprintf("INSERT INTO foods (uid, food) VALUES ('%d', '%s')", $_SESSION["uid"],
$food);
$result = mysql_query($query);
?>

i've been trying a number of different ways, first without using an array, but with each option as a separate variable, but i can't seem to get anything to work. any suggestions?
can't get that suggestion to work - see below

<form action = "choice2.php" method = "post">
<td class="field">Chicken Parmesean</td>
<td><input name = "chicken_parmesean" type="checkbox" value
= "Chicken Parmesean"/></td>
<tr>
<td class="field">Fried Plantains</td>
<td><input name = "fried_plantains" type ="checkbox" value =
"Fried Plantains"/></td>
</form>

<?
$chicken_parmesean = $_POST['chicken_parmesean']
$fried_plantains = $_POST['fried_plantains']

//prepare SQL - should insert true if checked, false if unchecked
$query = sprintf("INSERT INTO foods (uid, Chicken Parmesean, Fried
Plantains) VALUES ('%d', '%s', '%s')", $_SESSION["uid"],
$chicken_parmesean, $fried_plantains);
$result = mysql_query($query);

in the table i have a row for each different food, set as a bool, though i've also tried it as a string or text. 0 always is submitted

 
Reply With Quote
Revenue Shared Ads
(#2 (permalink))
Old
David W is Offline
Private
Points: 149, Level: 1 Points: 149, Level: 1 Points: 149, Level: 1
Activity: 0% Activity: 0% Activity: 0%
David W is an unknown quantity at this point
 
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jan 2008
January 1st, 2008

Funny thing, I've just been looking into this issue myself. What you might want is the mysql "set" datatype. Basically, you can define a single data element that encodes a set of up to 64 distinct values. You can associate those individual values to your checkboxes.

For example, you can define a set to represent "Fried Plantains," "Kiwi Fruit," "Fried Worms," etc, then record which values the user selected in that set variable.

The downside is that you're limited to 64 values. If you need more than that, you'd need to define several sets.

There are disadvantages to set variables, though. See the referenced link.

An alternative is to concatenate all of the selected values together into a string with a separator, for example "Fried Worms:Liver:Chocolate Cherries" for the values selected by the user. Searching is a bit ickier, but it can be done.

Hope this helps.

-------

You can use implode() and explode() to encode and decode an array of selected checkboxes into a single text string for storage in your table column.

 
Reply With Quote
Revenue Shared Ads
Reply

Bookmarks

Tags
checkboxes, database, mysql, submitting

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Rupees Per Thread View: 1.00
Rupees Per Thread: 15.00
Rupees Per Post: 5.00
Forum Jump



These are the 100 most searched terms
Search Cloud
_backup.rc antrix wow autocad 2007 keygen best google gadgets best wow character bt map devil may cry 4 crack donkey kong 64 rom erika bella fuckteam fuckteam 5 georgina lempin gigistar gigistar topless kpsden kpsforum left 4 dead comic left 4 dead comics leveling enchanting mangos item id mangos linux mangos server mangos spell id mangos wotlk mature creampie milena velba milena velba forum no rest for the ass object id wow phoenix marie questions to ask people this is sparta vipersdenforums voyage century bot windows activation workaround windows xp lite wotlk alpha private server wotlk private server wotlk server wotlk wiki wow leatherworking guide wow mount locations wow object id wow object id list wow object id's wow object ids wow objects id www.adobeflashplayer.com www.kpsforum.com xbox 360 mmorpg ... powered by Simple Search Cloud
Powered by vBulletin® Version 3.8.0 Release Candidate 1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright 2004-2009 KPsN


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83