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
SOLUTION
Avatar of Member_2_4694817
Member_2_4694817

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Loganathan Natarajan
You can concatenate with | or , the selected values and store them in text filed. So that you can split the entries back.
Avatar of Member_2_4694817
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?
Avatar of meerecat1981
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.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo