2014-01-09 119 views
-1

在這裏,我試圖插入我的記錄到數據庫使用下面的代碼。我在這裏遇到了一個奇怪的問題。當我插入DATAS我得到這些下面的錯誤PDO無法插入記錄

注意:未定義指數:city_state

注意:未定義指數:國家

查詢FailedSQLSTATE [23000]:完整性約束違規:1048列「GuestState」不能爲空

但是當我刪除這兩個$cstate = $_POST['city_state']$ccountry = $_POST['country']它會正確地插入剩餘DATAS。我是新來的PHP Pdo。我不知道我在這裏犯了什麼錯誤?爲什麼這兩件事不把數據存儲到數據庫?

$cstate = $_POST['city_state']; 
$ccountry = $_POST['country']; 
try 
    { 
     $db = $conn->prepare("INSERT INTO ebvouchers (VoucherReference, BookingDate, CheckIndate, CheckOutDate, MealPlanID_Fk, RoomTypeID_Fk, GuestName, TotalAdults, Childrens, Infants, GuestPhone, GuestEmail, GuestFax, GuestAddressLine1, GuestAddressLine2, GuestRegion, GuestCity, GuestState, GuestCountry, GuestCurrency, GuestCurrencyTo, CurrencyAmount, GuestCurrencyValue) VALUES (:reference, :refdate, :ccin, :cout, :r_key, :r_key, :gname, :totaladults, :childrens, :infants, :gphone, :gemail, :gfax, :gaddress1, :gaddress2, :gregion, :city, :state, :country, :currency, :currencyto, :amount, :currencyvalue)"); 
     $db->execute(array(':reference'=>$creference, ':refdate'=>$crefdate, ':ccin'=>$ccin, ':cout'=>$cout, ':r_key'=>$ref_key, ':gname'=>$cgname, ':totaladults'=>$ctotaladults, ':childrens'=>$cchildrens, ':infants'=>$cinfants, ':gphone'=>$cgphone, ':gemail'=>$cgemail, ':gfax'=>$cgfax, ':gaddress1'=>$cgaddress1, ':gaddress2'=>$cgaddress2, ':gregion'=>$cregion, ':city'=>$ccity, ':state'=>$cstate, ':country'=>$ccountry, ':currency'=>$ccurrency, ':currencyto'=>$ccurrencyto, ':amount'=>$camount, ':currencyvalue'=>$ccurrencyvalue)); 
    } 
    catch(PDOException $e) 
    { 
     echo 'Query Failed'.$e->getMessage(); 
    } 
+1

問題是,'$ _POST'不包括兩個值:'$ _POST ['city_state']'和'$ _POST ['country']' – Raptor

+1

回顯這兩個值並放置死亡; echo $ cstate = $ _POST ['city_state']; echo $ ccountry = $ _POST ['country'];死; 來檢查它們是否有價值... – urfusion

+0

還有一種方法可以在數據庫中設置默認值.. – urfusion

回答

1
Notice: Undefined index: city_state 

Notice: Undefined index: country 

這個錯誤本身指出city_state國家沒有在POST參數

定義的索引檢查你的HTML表單和發佈這些參數設置爲服務器,然後檢查。