Jump to content

How to store checkbox answers from html form into mysql?


skidmark10

Recommended Posts

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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')

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.