2011-04-15 35 views
0

我有一個PHP表單,當我將字段留空時,會發生錯誤。離開字段空白時出現PHP格式錯誤

對不起,在重複鍵更新clientID = '45',clientName ='Frank'時將此客戶端sql = insert插入db1.cleints(clientID,clientName)值(45,Frank)時出錯。在你的SQL語法中;檢查對應於你的MySQL服務器版本的手冊,在第1行'clientName'附近使用正確的語法。

更新:我已經包含了代碼是什麼。請忽略這一事實,我的上述只是一個修改的例子。

$sql = "insert into $DB.theater (dsc_id, dsc_lname, dsc_fname, distributor_id, distributor_name, distributor_theater_id, manager_id, manager_name, is_new_theater, name, phone_area, phone_exch, phone_local, phone_ext, street, city, state_prov, country, postal, web_site_url, manager_gender, theater_manager_name, email, location_desc, square_footage, year_founded, colors_techs, stylists, assistants, active_chairs, booth_renters, services, purchase_volume, products, other_lines, other_lines_txt, percent_women, demog_lt_20, demog_lt_35, demog_lt_55, demog_ge_55, avg_sale, avg_cut, avg_color, avg_hilite, rating, can_display, retail_sf, grade, dsc_notes) values ($dsc_id, '$dsc_lname', '$dsc_fname', $distributor_id, '$distributor_name', '$distributor_theater_id', $manager_id, '$manager_name', '$is_new_theater', '$name', '$phone_area', '$phone_exch', '$phone_local', '$phone_ext', '$street', '$city', '$state_prov','$country', '$postal', '$web_site_url', '$manager_gender', '$theater_manager_name', '$email', '$location_desc', '$square_footage', '$year_founded', '$colors_techs', '$stylists', '$assistants', '$active_chairs', '$booth_renters', '$services', '$purchase_volume', '$products', '$other_lines','$other_lines_txt', '$percent_women', '$demog_lt_20', '$demog_lt_35', '$demog_lt_55', '$demog_ge_55', '$avg_sale', '$avg_cut', '$avg_color', '$avg_hilite', '$rating', '$can_display', '$retail_sf', '$grade', '$dsc_notes') on duplicate key update dsc_id=$dsc_id, dsc_lname='$dsc_lname', dsc_fname='$dsc_fname', distributor_id=$distributor_id, distributor_name='$distributor_name', distributor_theater_id='$distributor_theater_id', manager_id=$manager_id, manager_name='$manager_name', is_new_theater='$is_new_theater', name='$name', phone_area='$phone_area', phone_exch='$phone_exch', phone_local='$phone_local', phone_ext='$phone_ext', street='$street', city='$city', state_prov='$state_prov', country='$country', postal='$postal', web_site_url='$web_site_url', manager_gender='$manager_gender', theater_manager_name='$theater_manager_name', email='$email', location_desc='$location_desc', square_footage='$square_footage', year_founded='$year_founded', colors_techs='$colors_techs', stylists='$stylists', assistants='$assistants', active_chairs='$active_chairs', booth_renters='$booth_renters', services='$services', purchase_volume='$purchase_volume', products='$products', other_lines='$other_lines', other_lines_txt='$other_lines_txt', percent_women='$percent_women', demog_lt_20='$demog_lt_20', demog_lt_35='$demog_lt_35', demog_lt_55='$demog_lt_55', demog_ge_55='$demog_ge_55', avg_sale='$avg_sale', avg_cut='$avg_cut', avg_color='$avg_color', avg_hilite='$avg_hilite', rating='$rating', can_display='$can_display', retail_sf='$retail_sf', grade='$grade', dsc_notes='$dsc_notes'"; 
+0

您需要顯示一些代碼。 – 2011-04-15 17:14:17

回答

3
insert into db1.cleints (clientID, clientName) 
values (45, Frank) 
on duplicate key update clientID='45', clientName='Frank' 

第一Frank沒有加引號。

另外,值得注意的是,on duplicate key update的整個目的是爲了能夠在不存在的情況下插入記錄,或者如果它已經存在,則更新它。您正嘗試使用它已有的相同值進行更新。也許你想INSERT IGNORE ...

回答到更新的問題:

我看來,我們不得不猜測。我的教育猜測是,你不是逃避輸入數據。相反,你將它注入到你的SQL代碼中。當數字字段留空時,不要用正確的NULL替換它;它只是一個轉換到(空)字符串:

echo "VALUES($one, $two, $three)"; 

...生成:

VALUES(1, , 3) 

它可能失敗,以及如果你鍵入這樣欄中輸入字母,或者如果你輸入O'Hara

相關問題