Forum Moderators: coopster

Message Too Old, No Replies

PHP MYSQL Checkbox problem

         

sharkmeat

3:39 pm on Jun 6, 2007 (gmt 0)

10+ Year Member



Hello,
I have 2 checkboxes that will POST to the next page but will not store in the database unless it is selected. Here is my code:

<label><input name="veggie1" value="1" type="checkbox">Thursday</label>


<label><input name="veggie2" value="1" type="checkbox">Friday</label>

On the 2nd page


$veggie1=$_POST['veggie1'];
$veggie2=$_POST['veggie2'];
mysql_query("INSERT INTO data (name, address, city, state, zip, phone,emails ,hospice_affiliation, discipline_code, is_member, pre_conf_day1, pre_conf_day2, pre_conf_day3, leadership_earlybird, leadership_registered,veggie1)
VALUES ('$name', '$address', '$city', '$state', '$zip', '$phone', '$emails', '$hospice', '$discipline', '$member', $confday1, $confday2, $confday3, $earlybird, $registered, $veggie1 )");

Just to let everyone know that these 3 variable ( $confday1, $confday2, $confday3) are also checkboxes that only work if selected also.

The entire script stops if all checkboxes are not selected and nothing is stored in the database.

They are being stored in a mysql database as a tinyint default 0.

What can I do to resolve this?

[edited by: jatar_k at 3:51 pm (utc) on June 6, 2007]
[edit reason] fixed sidescroll [/edit]

whoisgregg

3:48 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick check to see if the variable is set should resolve it. The example below uses the ternary operator [us2.php.net] to make the code a bit shorter.

$veggie1 = (isset($_POST['veggie1']) && $_POST['veggie1'] == '1')? 1 : 0; 
$veggie2 = (isset($_POST['veggie2']) && $_POST['veggie2'] == '1')? 1 : 0;

Also, you should make sure you are escaping user input with mysql_real_escape_string() [php.net] to prevent sql injection attacks.

sharkmeat

4:20 pm on Jun 6, 2007 (gmt 0)

10+ Year Member



Thank You! Worked like a charm. I do have another question. I am having the same problem with radio buttons but they have 9 different values such as A1, A2, A3 and so on...

I guess I have to make sure they are set, but I do not how I would write the code.

Thank You again!

whoisgregg

5:08 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, you would use the provided data:

$radio1 = (isset($_POST['radio1']))? $_POST['radio1'] : 'default'; 

You can also do that for checkboxes, actually. The extra check and forcing it to a 1 or 0 just skips the need to validate what is provided in the checkbox post array. Using the code above, you'll still need to use mysql_real_escape_string on the $radio1 value.

sharkmeat

7:04 pm on Jun 6, 2007 (gmt 0)

10+ Year Member



The radio button is still not working. It passes the value to the next page correctly via echo, but halts the entire script and does not input into the database.
the form code is as follows:
<label>
<input name="pre_conf_day1_track" value="A1" type="radio">
A1 </label> <label>
<input name="pre_conf_day1_track" value="A2" type="radio">
A2 </label> <label>
<input name="pre_conf_day1_track" value="A3" type="radio">
A3 </label> <label>
<input name="pre_conf_day1_track" value="A4" type="radio">
A4 </label> <label>
<input name="pre_conf_day1_track" value="A5" type="radio">
A5 </label> <label>
<input name="pre_conf_day1_track" value="A6" type="radio">
A6 </label> <label>
<input name="pre_conf_day1_track" value="A7" type="radio">
A7 </label> <label>
<input name="pre_conf_day1_track" value="A8" type="radio">
A8 </label> <label>
<input name="pre_conf_day1_track" value="A9" type="radio">
A9 </label>

The process.php is as follows:

$conftrack1 = (isset($_POST['pre_conf_day1_track']))? $_POST['pre_conf_day1_track'] : 'default';
mysql_query("INSERT INTO data (name, address, city, state, zip, phone, emails, hospice_affiliation, discipline_code, is_member, pre_conf_day1, conftrack1, pre_conf_day2, pre_conf_day3, leadership_earlybird, leadership_registered, veggie1, veggie2)
VALUES ('$name', '$address', '$city', '$state', '$zip', '$phone', '$emails', '$hospice', '$discipline', '$member', $confday1, $conftrack1, $confday2, $confday3, $earlybird, $registered, $veggie1, $veggie2 )");

The MYSQL database is TEXT NULL

[edited by: jatar_k at 9:14 pm (utc) on June 6, 2007]
[edit reason] fixed sidescroll [/edit]

Habtom

8:52 am on Jun 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mysql_query("INSERT INTO data (name, address, city, state, zip, phone, emails, hospice_affiliation, discipline_code, is_member, pre_conf_day1, conftrack1, pre_conf_day2, pre_conf_day3, leadership_earlybird, leadership_registered, veggie1, veggie2)
VALUES ('$name', '$address', '$city', '$state', '$zip', '$phone', '$emails', '$hospice', '$discipline', '$member', $confday1, $conftrack1, $confday2, $confday3, $earlybird, $registered, $veggie1, $veggie2 )");

TO

mysql_query("INSERT INTO data (name, address, city, state, zip, phone, emails, hospice_affiliation, discipline_code, is_member, pre_conf_day1, conftrack1, pre_conf_day2, pre_conf_day3, leadership_earlybird, leadership_registered, veggie1, veggie2)
VALUES ('$name', '$address', '$city', '$state', '$zip', '$phone', '$emails', '$hospice', '$discipline', '$member', '$confday1', '$conftrack1', '$confday2', '$confday3', '$earlybird', '$registered', '$veggie1', '$veggie2')")or die(mysql_error());

I have put the single quotes for you. See what kind of error it generates. If it just exits, it seems there was an error somewhere but couldn't display it.

Habtom

sharkmeat

12:50 pm on Jun 7, 2007 (gmt 0)

10+ Year Member



It was the single quotes. Thank You. Script is working perfect.