Avatar of meerecat1981
meerecat1981
 asked on

Storing a PHP Checkbox in a Database

Hello

I have a PHP page with text and Database fields.

This is the code that draws the checkbox:

<tr>
    <td width="43"><label for="business_type">Salon</label></td>
    <td width="30"><input type="checkbox" name="business_type[]" id="business_type" value="salon" /></td>
    <td width="38"><label for="business_type">Mobile</label></td>
    <td width="30"><input type="checkbox" name="business_type[]" id="business_type" value="mobile" /></td>
    <td width="53"><label for="business_type">Student</label></td>
    <td width="25"><input type="checkbox" name="business_type[]" id="business_type" value="student" /></td>
    <td width="37"><label for="business_type">Other</label></td>
    <td width="434"><input type="checkbox" name="business_type[]" id="business_type" value="other" /></td>
  </tr>

Open in new window


This is the code that stores the texts fields in the database:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<? 
 $trainee_name=$_POST['trainee_name']; 
 $company_name=$_POST['company_name']; 
 $address=$_POST['address']; 
  $telephone=$_POST['telephone']; 
 $email_address=$_POST['email_address']; 
 $website=$_POST['website'];
  $trainer_name=$_POST['trainer_name']; 
 $training_date=$_POST['training_date']; 
 $trainer_notes=$_POST['trainer_notes'];
 $area_consultant=$_POST['area_consultant'];
 mysql_connect("localhost", "web46-training", "training") or die(mysql_error()); 
 mysql_select_db("web46-training") or die(mysql_error()); 
 mysql_query("INSERT INTO `data` VALUES ('$trainee_name', '$company_name', '$address', '$telephone', '$email_address', '$website', '$trainer_name', '$training_date', '$trainer_notes', '$area_consultant')"); 
 Print "Your information has been successfully added to the database."; 
 ?> 
</body>
</html>

Open in new window


How do I store which of the php checkboxes are checked into the database.  What kind of field do I need to create in the database table.  All my text fields are Varchar.  I was thinking maybe bit?

Many Thanks
PHPMySQL Server

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
SOLUTION
Member_2_4694817

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Loganathan Natarajan

You can concatenate with | or , the selected values and store them in text filed. So that you can split the entries back.
Member_2_4694817

BTW, you do know that you should be more careful with handing input data to mySQL?
What if someone maliciously entered
foo'); truncate table data;

Open in new window

in the consultant field?
meerecat1981

ASKER
Are you able to show me an example? I'm new to this.

Enterint the above code did nothing to the database.  Isn't that because a VARCHAR field is purely for storing text? Please feel free to tell me i'm talking rubbish lol.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ray Paseur

I tried to post a link to this article earlier, but apparently it did not "take," so here it is...

https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html

HTH, ~Ray
Ray Paseur

Wow, what was wrong with the answers?  You did not even ask for clarifying information - you just gave a marked-down grade.  Please see the grading guidelines here:
https://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26

Please tell us what you expected and why you didn't respond if you thought our posts did not have enough information for your needs.  thanks, ~Ray