-4

I encounterd the following error while attempting to insert data into my table:

[1064] 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 '2','15','2013','12','5','51','PM','6','15','44','PM')' at line 2

I am using multiple check boxes in the form. How can I correct this error?

error while inserting : [1064] 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 '2','15','2013','12','5','51','PM','6','15','44','PM')' at line 2

I m using multiple checkboxes in the form

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="shree"; // Database name 
$tbl_name="order_people"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form


$first=$_POST['first'];
$last=$_POST['last'];
$email=$_POST['email'];
$number=$_POST['number'];
$address=$_POST['address'];
$address1=$_POST['address1'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$country=$_POST['country'];
$event=$_POST['event'];
$package=$_POST['package'];
$food_type=$_POST['food_type'];
$menu=$_POST['menu'];
$starters=$_POST['starters'];
$cold_drinks=$_POST['cold_drinks'];
$fast_food=$_POST['fast_food'];
$gujrati=$_POST['gujrati'];
$jain=$_POST['jain'];
$marathi=$_POST['marathi'];
$chinese=$_POST['chinese'];
$punjabi=$_POST['punjabi'];
$south_indian=$_POST['south_indian'];
$desserts=$_POST['desserts'];
$month=$_POST['month'];
$date=$_POST['date'];
$year=$_POST['year'];
$hours=$_POST['hours'];
$minutes=$_POST['minutes'];
$seconds=$_POST['seconds'];
$ampm=$_POST['ampm'];
$hours1=$_POST['hours1'];
$minutes1=$_POST['minutes1'];
$seconds1=$_POST['seconds1'];
$ampm1=$_POST['ampm1'];

$event = implode(",", $_POST['event']);
$food_type = implode(",", $_POST['food_type']);
$starters = implode(",", $_POST['starters']);
$cold_drinks = implode(",", $_POST['cold_drinks']);
$fast_food = implode(",", $_POST['fast_food']);
$jain = implode(",", $_POST['jain']);
$gujrati = implode(",", $_POST['gujrati']);
$marathi = implode(",", $_POST['marathi']);
$chinese = implode(",", $_POST['chinese']);
$punjabi = implode(",", $_POST['punjabi']);
$south_indian = implode(",", $_POST['south_indian']);
$desserts = implode(",", $_POST['desserts']);


// Insert data into mysql 
$sql = "INSERT INTO $tbl_name(first,last,email,number,address,address1,city,state,zip,country,event,package,food_type,menu,starters,cold_drinks,fast_food,jain,gujrati,marathi,chinese,punjabi,south_indian,desserts)
VALUES('$first','$last','$email','$number','$address','$address1','$city','$state','$zip','$country','$event','$package','$food_type','$menu','$starters','$cold_drinks','$fast_food','$jain','$gujrati','$marathi','$chinese','$punjabi','$south_indian','$desserts)";

echo ($sql);

$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='form.php'>Back to main page</a>";
}

else {
echo 'error while inserting : ['.mysql_errno().'] '.mysql_error();;
}
?> 

SQL

INSERT INTO order_people(
    first,last,email,number,address,address1,city,state,zip,country,
    event,package,food_type,menu,starters,cold_drinks,fast_food,jain,
    gujrati,marathi,chinese,punjabi,south_indian,desserts
) VALUES(
    'Akshay','Desai','akshaydesai@mail.com','99846464','dihqwolhwoh','efhowhefop',
    'sifgoigfo','maharashtra','mumbai','India','Birthday Parties','1','Non-Vegetarian','',
    'Wontons Crab and Goons,Cucumber Mousse,Baked Shrimp Rangoon','Soft Drinks,Smoothies',
    'Aloo Tikki,Bhajiya,Bhel','Green Gram Dhokla,Jain Gawar Ki Sabji,Jain Spicy Sprouts Pulav,Jain Upma',
    'Stuffed Lady Finger,Surti Papdi Shaak,Dahi Vada,Singoda Na Bhajia','Chicken Biryani,Aloo Vadi,Bharli Vangi',
    'Chinese Noodles,Chicken with Brocoli and Rice,Chinese Veg Noodles','Aloo Amritsari,Chana Dal Paratha,Punjabi Kadi Pakoda,Punjabi Kadhi',
    '1,1','Fruit Dish,Coconut Pudding,Chocolate Banana Parfaits
)

error while inserting:

[1064] 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 ''Fruit Dish,Coconut Pudding,Chocolate Banana Parfaits)' at line 2

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

You have an error in building your query:

// Insert data into mysql 
$sql = "INSERT INTO $tbl_name(
  first,last,email,number,address,address1,city,state,zip,country,event,package,food_type,menu,starters,cold_drinks,fast_food,jain,gujrati,marathi,chinese,punjabi,south_indian,desserts)
VALUES(
  '$first',
  '$last',
  '$email',
  '$number',
  '$address',
  '$address1',
  '$city',
  '$state',
  '$zip',
  '$country',
  '$event',
  '$package',
  '$food_type',
  '$menu',
  '$starters',
  '$cold_drinks',
  '$fast_food',
  '$jain',
  '$gujrati',
  '$marathi',
  '$chinese',
  '$punjabi',
  '$south_indian',
  '$desserts
------------^
)";

You missed one ' at the end. This should be:

  '$south_indian',
  '$desserts'
)";

By the way please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
j0k
  • 22,600
  • 28
  • 79
  • 90