无法识别我在PHP中创建的查询中的MySQL错误

时间:2014-05-17 14:37:58

标签: php mysql sql

我有这个问题:

$sql = 'insert into scheduled_hikes ( organizer_member_id , hike_group_id , hike_title , hike_description , 
hike_date , end_date , dogs_allowed , mileage , feet_elevation , start_time , end_time , recommended_clothing , recommended_food , 
cancel_condition , contact_phone , show_on_home_page , park_name , full_address , lng , lat , hike_privacy)          

values ( '.$member_id.' , '.$hiking_group_id.' , "'.$hike_name.'" , "'.$hike_description.'" ,
 STR_TO_DATE( "'.$date_string.'" , "%m-%d-%Y" ) , STR_TO_DATE( "'.$date_string2.'" , "%m-%d-%Y" ) , '.$dog_radio.' , '.$mileage.' , '.$feet_elevation.' , "'.$start_time.'" ,
 "'.$end_time.'" , "'.$recommended_clothing.'" , "'.$recommended_food.'" , "'.$cancel_condition.'" , "'.$contact_phone.'", '.$show_on_home_page.' , "'.$park_name.'" ,  "'.$full_address.'" , '.$lng.' , '.$lat.' , '.$hike_privacy.' )';

我得到了关于它的错误:

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 ' , 1 )' at line 8

我似乎无法识别查询有什么问题。有人能够发现问题所在吗?

以下是SQL的输出:

insert into scheduled_hikes ( organizer_member_id , hike_group_id , hike_title , hike_description , hike_date , end_date ,
                                                dogs_allowed , mileage , feet_elevation , start_time , end_time , recommended_clothing , recommended_food ,
                                                cancel_condition , contact_phone , show_on_home_page , park_name , full_address , lng , lat , hike_privacy)
                                                 values ( 2 , -1 , "NY Hike" ,
                                                 "Manhattan" ,
                                                 STR_TO_DATE( "05-30-2014" , "%m-%d-%Y" ) , STR_TO_DATE( "--" , "%m-%d-%Y" ) , 1 , 3 , 0 , "4:00 PM" ,
                                                 "" , "" , " " , "  " , ""
                                                 , 1 , "" ,  "Manhattan" ,  ,
                                                 , 1 )

谢谢!

4 个答案:

答案 0 :(得分:3)

您需要设置变量$lng$lat。它们是空的,因此您最终会在查询中连续使用两个逗号,这是无效的。

您还应该检查设置$date_string2$end_time的代码。 $date_string2设置为"--",这不是有效日期,$end_time是一个空字符串(我不知道您的应用程序中是否正常)。很多其他变量也是空的,但也许没关系。

答案 1 :(得分:1)

当您在查询中传递字符串类型的php变量时,您应该使用'而不是"来转义它们(当您传递数字类型的变量时根本不需要它们):< / p>

$sql = "INSERT INTO scheduled_hikes 
         ( organizer_member_id
         , hike_group_id
         , hike_title 
         , hike_description
         , hike_date
         , end_date
         , dogs_allowed
         , mileage
         , feet_elevation
         , start_time
         , end_time
         , recommended_clothing
         , recommended_food
         , cancel_condition
         , contact_phone
         , show_on_home_page
         , park_name
         , full_address
         , lng
         , lat
         , hike_privacy)
        VALUES 
         ( '".$member_id."' 
         , '".$hiking_group_id."'
         , '".$hike_name."'
         ,  '".$hike_description."'
         , STR_TO_DATE( '".$date_string."' , '%m-%d-%Y' )
         , STR_TO_DATE( '".$date_string2."' , '%m-%d-%Y' )
         , '".$dog_radio."'
         , '".$mileage."'
         , '".$feet_elevation."'
         , '".$start_time."'
         , '".$end_time."'
         , '".$recommended_clothing."'
         , '".$recommended_food."'
         , '".$cancel_condition."'
         , '".$contact_phone."'
         , '".$show_on_home_page."'
         , '".$park_name."'
         , '".$full_address."'
         , '".$lng."'
         , '".$lat."'
         , '".$hike_privacy."' )";

答案 2 :(得分:1)

我认为问题是引号和引号的混合。此外,在我看来,整个“停止 - 开始”的事情(“。”)是不必要的 - 并且在一条线上全部写出来都没有帮助。试试这个......

 "
INSERT INTO scheduled_hikes 
(organizer_member_id 
,hike_group_id 
,hike_title 
,hike_description 
,hike_date 
,end_date
,dogs_allowed 
,mileage 
,feet_elevation 
,start_time 
,end_time 
,recommended_clothing 
,recommended_food 
,cancel_condition 
,contact_phone 
,show_on_home_page 
,park_name
,full_address 
,lng 
,lat 
,hike_privacy
) VALUES 
('$member_id' 
,'$hiking_group_id'
,'$hike_name'" 
,'$hike_description'
, STR_TO_DATE('$date_string','%m-%d-%Y') 
, STR_TO_DATE('$date_string2','%m-%d-%Y') 
,'$dog_radio'
,'$mileage'
,'$feet_elevation'
,'$start_time'
,'$end_time'
,'$recommended_clothing' 
,'$recommended_food' 
,'$cancel_condition' 
,'$contact_phone'
,'$show_on_home_page'
,'$park_name' 
,'$full_address'
,'$lng'
,'$lat'
,'$hike_privacy'
);
";

此外,应该不用说你应该使用mysqli或PDO,而且至关重要的是,准备好了陈述。

答案 3 :(得分:0)

如果您使用双引号(&#34;),则可以直接在此字符串中使用变量。

$sql = "insert into scheduled_hikes ( organizer_member_id , hike_group_id , hike_title , hike_description , hike_date , end_date , dogs_allowed , mileage , feet_elevation , start_time , end_time , recommended_clothing , recommended_food , cancel_condition , contact_phone , show_on_home_page , park_name , full_address , lng , lat , hike_privacy)

values ($member_id, $hiking_group_id, $hike_name, $hike_description,".STR_TO_DATE($date_string,"%m-%d-%Y").",".STR_TO_DATE($date_string2,"%m-%d-%Y").",$dog_radio, $mileage, $feet_elevation, $start_time, $end_time, $recommended_clothing, $recommended_food, $cancel_condition, $contact_phone, $show_on_home_page, $park_name, $full_address, $lng, $lat, $hike_privacy)";