PDO无法插入记录

时间:2014-01-09 06:47:26

标签: php pdo

这里我试图使用下面的代码将我的记录插入数据库。我在这里遇到了一个奇怪的问题。当我插入数据时,我得到以下错误

  

注意:未定义的索引:city_state

     

注意:未定义的索引:国家/地区

     

查询失败的SQLSTATE [23000]:完整性约束违规:1048列'GuestState'不能为空

但是当我删除这两个$cstate = $_POST['city_state']$ccountry = $_POST['country']时,它会正确地插入剩余的数据。我是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 个答案:

答案 0 :(得分:1)

Notice: Undefined index: city_state

Notice: Undefined index: country

此错误本身表明 city_state 国家/地区未在POST参数中定义索引

检查您的HTML表单并将这些参数发布到服务器,然后检查。