执行插入语句时出现1064错误


Error 1064 when executing an insert statement

在尝试向表中插入数据时遇到以下错误:

[1064]你的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第2行'2','15','2013','12','5','51','PM','6','15','44','PM')'附近使用正确的语法

我在表单中使用多个复选框。我怎样才能纠正这个错误?

[1064]你的SQL语法有一个错误;检查与MySQL服务器版本对应的手册,以便在第2行'2','15','2013','12','5','51','PM','6','15','44','PM')'附近使用正确的语法

我在表单

中使用了多个复选框
<?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
)

插入错误:

[1064]你的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第2行"Fruit Dish,Coconut Pudding,Chocolate Banana Parfaits"附近使用正确的语法

在构建查询时出现错误:

// 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
------------^
)";

你在最后漏掉了一个'。这应该是:

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

顺便说一下,请不要在新代码中使用mysql_*函数。它们不再被维护,并被正式弃用。见红框?学习预处理语句,并使用PDO或MySQLi——本文将帮助您决定使用哪一种。如果你选择PDO,这里有一个很好的教程。