Page 1 of 1

Checkboxes - Storing data in mysql

Posted: Fri Sep 30, 2005 8:51 pm
by jjfletch
Hi there,

I have a form that uses checkboxes, but I'm having a problem storing the data in MySQL.

First, I'm looking up values in a table to create the checkboxes:

Code: Select all

function fn_1 () 
{ 
      echo "QUESTION_ONE";
           $query = "SELECT DISTINCT(fn_1) FROM choices where fn_1!=''";
           $result = mysql_query ($query); 
           while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
              echo "<dt><input type=\"checkbox\" id=\"fn_set_1[]\" value=\"{$row['fn_1']}\">{$row['fn_1']}<br />\n";
           }
      echo "<p>";
}
Then, I try this:

Code: Select all

if (isset($_POST['fn_set_1'])) {
                $FN1=implode(',',$_POST['fn_set_1']);   
        } else {
                $FN1=''; 
        }
But, when I try to insert the data into MySQL, it inserts the blank (even when the boxes are checked).

Can someone help out here?

Posted: Fri Sep 30, 2005 10:15 pm
by Skara
Try print_r($_POST); to see what's set and what's not.
Double check your output html code to see that it's printed right.
Echo $FN1 to see what it's set to.
Echo your INSERT query to make sure it's set right.

btw, this:

Code: Select all

mysql_fetch_array($result, MYSQL_ASSOC)
is better accomplished as so:

Code: Select all

mysql_fetch_assoc($result)
ditto for mysql_fetch_row().

Posted: Fri Sep 30, 2005 10:38 pm
by jjfletch
Thanks for the tip!

I'm still having problems. (So.... frustrated.... :x )

>>> Try print_r($_POST); to see what's set and what's not.

Did that, and nothing is printed for that particular field, even when the boxes are checked. It will print out everything else that's selected on the form though.

>>> Double check your output html code to see that it's printed right.

It looks correct. I even typed it manually just to make sure.

>>> Echo $FN1 to see what it's set to.

Nothing prints.


>>> Echo your INSERT query to make sure it's set right.

It works fine. In fact, everything else is successfully being inserted into the database.

I'm pretty sure that it's echoing '', as stated in my else statement, but I can't figure out why. The checkboxes are checked, the values look correct.... I'm going insane!

Any suggestions?

Thanks, your help is greatly appreciated!

Posted: Fri Sep 30, 2005 10:44 pm
by Skara
Did that, and nothing is printed for that particular field, even when the boxes are checked. It will print out everything else that's selected on the form though.
= error. No reason to check anything else. :P

Make sure you have your html form set up properly. Should be in the format:
<form method='post'>
<input type='thetype' name='thename' /> * N
<input type='submit' value='Submit' />
</form>

If you're certain it's right, I don't know what to tell you. Could be a server setting that's blocking the $_POST variables.

Posted: Fri Sep 30, 2005 10:45 pm
by feyd
there's no name attribute set for the checkbox field, that's why you do not get any information in the print_r()

Posted: Fri Sep 30, 2005 10:50 pm
by Skara
*looks* hahahaha... missed that! Yeah, id usually doesn't have much use except for css. Replace that with name and you're running. ;)