Collect checkbox data in PHP/HTML/MySQL

I am setting up a form that gather different information. I need basic information, name, address, etc. which is done through text entry but then I need a bunch of information that is done by checkbox. What PHP owuld I use to get the  checkbox to record to the MySQL database if I have set the DB types to boolean (tinyint)? Here is what I am using for the text portions, LOA is the checkbox I can't get to work:

<?php
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$CWID = $_REQUEST['CWID'];
$LOA = $_REQUEST['LOA'];
if ((isset($_REQUEST['firstname'])) && (isset($_REQUEST['lastname'])) && (isset($_REQUEST['CWID'])) && (isset($_REQEUST['LOA'])))
{
 
if($result = mysql_query("INSERT INTO student (firstname, lastname, CWID) VALUES('$firstname', '$lastname', '$CWID') ") )
{
echo "Thank you, Your information has been entered into our database";
}
else
{
echo "ERROR: ".mysql_error();
}
 
bpfsrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

laurin1Commented:
First of all, LOA is not in the SQL code you've displayed above. Are you aware of that? If you are, can I see the HTML you are using?
0
Eddie ShipmanAll-around developerCommented:
Try this:
<?php
if ((isset($_REQUEST['firstname'])) && 
    (isset($_REQUEST['lastname']))  && 
    (isset($_REQUEST['CWID']))      && 
    (isset($_REQUEST['LOA']))) 
{
  $firstname = $_REQUEST['firstname'];
  $lastname  = $_REQUEST['lastname'];
  $CWID      = $_REQUEST['CWID'];
  $LOA       = $_REQUEST['LOA'];
  if ($result = mysql_query('INSERT INTO student (firstname, lastname, CWID, LOA) VALUES('.$firstname.', '.$lastname.', '.$CWID.', '.$LOA.')'))
  {
    echo 'Thank you, Your information has been entered into our database.';
  } else {
    echo 'ERROR: '.mysql_error();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
waygoodCommented:
WARNING: checkboxes are only submitted if they are ticked.

<input type="checkbox" name="LOA" value="1">

you can always do the following to check for empty values..

<input type="hidden" name="LOA" value="0"/>
<input type="checkbox" name="LOA" value="1"/>

This will overwrite the hidden LOA value of 0, IF the checkbox is ticked.
OR

In the processing part, predefine LOA as zero and only set it if a value has been given.
$LOA = 0;
if(isset($_REQUEST['LOA'])) $LOA = intval($_REQUEST['LOA']);  // see quotes below


ALSO enclose the textfields in quotes

  if ($result = mysql_query('INSERT INTO student (firstname, lastname, CWID, LOA) VALUES("'.$firstname.'", "'.$lastname.'", "'.$CWID.'", '.$LOA.')'))

0
waygoodCommented:
ALSO ALSO...

make the values safe for a query by escaping any quotes included in the data using mysql_real_escape_string

http://uk3.php.net/mysql_real_escape_string
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.