Checkboxes - Storing data in mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jjfletch
Forum Newbie
Posts: 13
Joined: Mon Apr 18, 2005 5:42 am

Checkboxes - Storing data in mysql

Post 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?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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().
jjfletch
Forum Newbie
Posts: 13
Joined: Mon Apr 18, 2005 5:42 am

Post 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!
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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()
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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. ;)
Post Reply