执行insert语句时出错1064

时间:2013-05-24 19:10:25

标签: php mysql

我在尝试将数据插入表格时遇到以下错误:

  

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

我在表单中使用了多个复选框。如何更正此错误?

  插入时出现

错误:[1064]您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在'2','15','2013','12','5','51','PM','6',''附近使用正确的语法。第2行的'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”附近正确的语法

1 个答案:

答案 0 :(得分:1)

构建查询时出错:

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

顺便提一句please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial