Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

declaring a checkbox in php and then updating a mysql db

Status
Not open for further replies.

bhavsta

Programmer
Feb 6, 2007
7
GB
Hi Guys,

I have a html for that consists of drop down menus, text boxes and two checkboxs. I can get all the data to be sent to a mysql db via php. But when i try to insert the checkboxes into the db i get an error stating the following:

Could not insert data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES ('6','a1','b1','3','c1','1234','1234','1234@1234.com

the code for my html form (checkboxes) is below:

<input type="checkbox" border="0" name="dprotection_one" value="1" />

<input type="checkbox" border="0" name="dprotection_two" value="2" />

i have declared the boxes in the php script as follows:

$dprotection_one=$_POST['dprotection_one'];
$dprotection_two=$_POST['dprotection_two'];


Please can someone help me.

Regards,

Bhav
 
Can we see your complete query, and how you are putting the checkbox values in it?

Also you should know that if a checkbox is not checked, then php will not create the POST variable for it.

so if dprotection_one is not checked but dprotection_two is, you'll only get $_POST['dprotection_two'] as a POST variable.

If you try to use $_POST['dprotection_one'] you'll get an error.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The trick about check boxes in PHP is they return a value of "on" if checked, and nothing if unchecked. You need to convert this to a boolean value... For MySQL I use...
Code:
if($Chkbx== "on")
    $Chkbx = 1;
else
    $Chkbx = 0;
 
DECLARATIONS

$salutation=$_POST['salutation'];
$first_name=$_POST['first_name'];
$surname=$_POST['surname'];
$status=$_POST['status'];
$job_title=$_POST['job_title'];
$DoB=$_POST['DoB'];
$telephone=$_POST['telephone'];
$email_address=$_POST['email_address'];
$university_college_school=$_POST['university_college_school'];
$start=$_POST['start'];
$end=$_POST['end'];
$dprotection_one=$_POST['dprotection_one'];
$dprotection_two=$_POST['dprotection_two'];


CREATING TABLE

$create ="CREATE table Registration_Data (salutation varchar(50),
first_name varchar(50),
surname varchar(50),
job_title varchar(50),
status varchar(30),
DoB varchar(30),
telephone varchar(20),
email varchar(50),
university_college_school varchar(30)
start varchar(15),
end varchar(15),
dprotection_one CHAR(1),
dprotection_two CHAR(1))";
mysql_query($create);

QUERY INSERT

$query = "INSERT INTO Registration_Data (salutation,
first_name,
surname,
job_title,
status,
DoB,
telephone,
email,
university_college_school,
start,
end,
dprotection_one,
dprotection_two,) VALUES
('".$salutation."','".$first_name."','".$surname."','".$status."','".
$job_title."','".$DoB."','".$telephone."','".$email_address."','".
$university_college_school."','".$start."','".$end."','".$dprotection_one.
"','".$dprotection_two."')";
if (mysql_query($query)) {
header("Location: exit();
} else {
die ("Could not insert data: " . mysql_error());
}
 
Hi Bam720,

How would i declare this as a variable, the if statement makes sense, however, how can i declare this and then get the results to insert into a mysql db after is has been picked up by the php?

Regards,

Bhavesh

Thank you for your help so far.
 
Code:
$dprotection_one=$_POST['dprotection_one'];
$dprotection_two=$_POST['dprotection_two']; 

if($dprotection_one== "on")
    $dprotection_one = 1;
else
    $dprotection_one = 0;

if($dprotection_two== "on")
    $dprotection_two = 1;
else
    $dprotection_two = 0;

You insert statement will work fine now
 
surely the first two lines of bam720's response will throw warnings in the event a checkbox is not checked. These may or may not be visible depending on your setting for display_errors

i would recommend preprofiling your forms (have the controls in an array that you use to create the output, validate and process the input.

absent of a profiled form i'd recommend rather using this construct

Code:
$dprotection_one= isset($_POST['dprotection_one']) ? 1 : 0;
 
thanx alot people i have cracked it!!!! in the end i ended up using the variable type as a tinyint in the declaration for dprotection. this seemed to do the trick. it even allowed blank ticks to be parsed and to be written into the db!!!! wonderfull :)

thanx again to everyone who helped me.

Regards,

Bhav
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top