skidmark10 Posted July 29, 2011 Share Posted July 29, 2011 Hello, I've been trying to insert checkbox answers from an html form into a mysql database. Any way to do this? Is there a specific code for this? On my html form, my radio buttons and drop-down boxes work just fine, so I assume there is something different I have to do for checkbox answers. I am using MySql 5.1.52, phpmyadmin 3.3.10.2 and php 5.2.17. If you need the code, just let me know. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/243148-how-to-store-checkbox-answers-from-html-form-into-mysql/ Share on other sites More sharing options...
mhallmon Posted July 29, 2011 Share Posted July 29, 2011 You should post your code so that people can see what your working with Quote Link to comment https://forums.phpfreaks.com/topic/243148-how-to-store-checkbox-answers-from-html-form-into-mysql/#findComment-1248875 Share on other sites More sharing options...
skidmark10 Posted July 29, 2011 Author Share Posted July 29, 2011 You should post your code so that people can see what your working with Here is my HTML <p>Please select all body types you would be interested in dating:</p> <table class=MsoNormalTable border=0 cellpadding=0 width="100%" style='width:100.0%;mso-cellspacing:1.5pt;mso-yfti-tbllook:1184;mso-padding-alt: 0in 5.4pt 0in 5.4pt'> <tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'> <td width="100%" style='width:100.0%;padding:.75pt .75pt .75pt .75pt'> <p class=MsoNormal><span style='mso-fareast-font-family:"Times New Roman"'><INPUT TYPE="checkbox" NAME="partnerbodytype[]" VALUE="Slender"> Slender <o:p></o:p></span></p> </td> </tr> <tr style='mso-yfti-irow:1'> <td width="100%" style='width:100.0%;padding:.75pt .75pt .75pt .75pt'> <p class=MsoNormal><span style='mso-fareast-font-family:"Times New Roman"'><INPUT TYPE="checkbox" NAME="partnerbodytype[]" VALUE="Average"> Average <o:p></o:p></span></p> </td> </tr> <tr style='mso-yfti-irow:2'> <td width="100%" style='width:100.0%;padding:.75pt .75pt .75pt .75pt'> <p class=MsoNormal><span style='mso-fareast-font-family:"Times New Roman"'><INPUT TYPE="checkbox" NAME="partnerbodytype[]" VALUE="Athletic"> Athletic <o:p></o:p></span></p> </td> </tr> <tr style='mso-yfti-irow:3;mso-yfti-lastrow:yes'> <td width="100%" style='width:100.0%;padding:.75pt .75pt .75pt .75pt'> <p class=MsoNormal><span style='mso-fareast-font-family:"Times New Roman"'><INPUT TYPE="checkbox" NAME="partnerbodytype[]" VALUE="Heavy"> Heavy <o:p></o:p></span></p> </td> </tr> </table> </div> </div> And here is my php $partnersbodytype = $_POST['partnersbodytype'] $sqlClientInfo= "INSERT INTO `Client_Info` (Partners_Body_Type) VALUES ($partnersbodytype) Quote Link to comment https://forums.phpfreaks.com/topic/243148-how-to-store-checkbox-answers-from-html-form-into-mysql/#findComment-1248879 Share on other sites More sharing options...
mhallmon Posted July 29, 2011 Share Posted July 29, 2011 You could assign an individual name to each value... make sure to update your database table as well: <td><div align="left">Please select all body types you would be interested in dating: </div></td> <td><input name="partnerbodytype1" type="checkbox" value="Slender" /> Slenders <input name="partnerbodytype2" type="checkbox" value="Average" /> Average<br /> <input name="partnerbodytype3" type="checkbox" value="Athletic" /> Athletic <input name="partnerbodytype4" type="checkbox" value="Heavy" /> Heavy<br /> php: $partnersbodytype1 = $_POST['partnersbodytype1'] $partnersbodytype2 = $_POST['partnersbodytype2'] $partnersbodytype3 = $_POST['partnersbodytype3'] $partnersbodytype4 = $_POST['partnersbodytype4'] $sqlClientInfo= "INSERT INTO `Client_Info` (partnersbodytype1, partnersbodytype2, partnersbodytype3, partnersbodytype4) VALUES ('$partnersbodytype1','$partnersbodytype2','$partnersbodytype3','$partnersbodytype4') Quote Link to comment https://forums.phpfreaks.com/topic/243148-how-to-store-checkbox-answers-from-html-form-into-mysql/#findComment-1248883 Share on other sites More sharing options...
skidmark10 Posted July 29, 2011 Author Share Posted July 29, 2011 You could assign an individual name to each value... make sure to update your database table as well: <td><div align="left">Please select all body types you would be interested in dating: </div></td> <td><input name="partnerbodytype1" type="checkbox" value="Slender" /> Slenders <input name="partnerbodytype2" type="checkbox" value="Average" /> Average<br /> <input name="partnerbodytype3" type="checkbox" value="Athletic" /> Athletic <input name="partnerbodytype4" type="checkbox" value="Heavy" /> Heavy<br /> php: $partnersbodytype1 = $_POST['partnersbodytype1'] $partnersbodytype2 = $_POST['partnersbodytype2'] $partnersbodytype3 = $_POST['partnersbodytype3'] $partnersbodytype4 = $_POST['partnersbodytype4'] $sqlClientInfo= "INSERT INTO `Client_Info` (partnersbodytype1, partnersbodytype2, partnersbodytype3, partnersbodytype4) VALUES ('$partnersbodytype1','$partnersbodytype2','$partnersbodytype3','$partnersbodytype4') Yikes! Is this the only way???? If so, I have to prepare myself for a LONG night of work.... Quote Link to comment https://forums.phpfreaks.com/topic/243148-how-to-store-checkbox-answers-from-html-form-into-mysql/#findComment-1248885 Share on other sites More sharing options...
mhallmon Posted July 29, 2011 Share Posted July 29, 2011 This isn't the only way... just an easy way. With [] I've always ran into nothing but issues. Shouldnt take long to add 3 fields to your db and change your code a little. You would be removing more code than adding. Quote Link to comment https://forums.phpfreaks.com/topic/243148-how-to-store-checkbox-answers-from-html-form-into-mysql/#findComment-1248891 Share on other sites More sharing options...
Psycho Posted July 29, 2011 Share Posted July 29, 2011 This isn't the only way... just an easy way. With [] I've always ran into nothing but issues. Shouldnt take long to add 3 fields to your db and change your code a little. You would be removing more code than adding. That is NOT easy, that is the laborious way of doing it. Just because you have trouble working with arrays, doesn't mean it is hard. You just need to push through that barrier. Not to mention your example query is wrong. You can't put all the values into a single parenthesized group. Sample code using the checkboxes named as arrays //Check if any checkboxes were checked if(isset($_POST['partnersbodytype'])) { //Parse the post values into query values $bodytypeValues = array(); foreach($_POST['partnersbodytype'] as $value) { $bodytypeValues = "(" . mysql_real_escape_string(trim($value)) . ")"; } //Create and run the insert query $query= "INSERT INTO `Client_Info` (Partners_Body_Type) VALUES " . implode(', ', $bodytypeValues); $result = mysql_query($query); } Now you can have 1 or 1,000 checkboxes and they will all be processed with those few lines of code. Quote Link to comment https://forums.phpfreaks.com/topic/243148-how-to-store-checkbox-answers-from-html-form-into-mysql/#findComment-1248902 Share on other sites More sharing options...
skidmark10 Posted July 30, 2011 Author Share Posted July 30, 2011 You could assign an individual name to each value... make sure to update your database table as well: <td><div align="left">Please select all body types you would be interested in dating: </div></td> <td><input name="partnerbodytype1" type="checkbox" value="Slender" /> Slenders <input name="partnerbodytype2" type="checkbox" value="Average" /> Average<br /> <input name="partnerbodytype3" type="checkbox" value="Athletic" /> Athletic <input name="partnerbodytype4" type="checkbox" value="Heavy" /> Heavy<br /> php: $partnersbodytype1 = $_POST['partnersbodytype1'] $partnersbodytype2 = $_POST['partnersbodytype2'] $partnersbodytype3 = $_POST['partnersbodytype3'] $partnersbodytype4 = $_POST['partnersbodytype4'] $sqlClientInfo= "INSERT INTO `Client_Info` (partnersbodytype1, partnersbodytype2, partnersbodytype3, partnersbodytype4) VALUES ('$partnersbodytype1','$partnersbodytype2','$partnersbodytype3','$partnersbodytype4') I've tried this way, but my answers still don't appear. Is there something wrong with my table? Is there something I can change with the table? Quote Link to comment https://forums.phpfreaks.com/topic/243148-how-to-store-checkbox-answers-from-html-form-into-mysql/#findComment-1249572 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.