我对PHP非常熟悉,但这是我第一次使用mysqli库。我试图在数据库中插入一行,它只是不插入。我的代码看起来像这样(为安全起见,数据库登录详细信息已更改):
... // $_REQUEST variables processed
$oConn = new mysqli("localhost","user","password","mydatabase") or die("Error " . mysqli_error($oConn));
$rProvider = $oConn->query("select * from providers where id = $iProviderID");
$aProvider = mysqli_fetch_array($rProvider);
// $aProvider has all the information from the provider that I wanted, so my database connection is working
$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values
($iProviderID, $iRatesID, $sSecret, '$sDate', '$sAltDate', $iAdults, $iChildren, $iTransfer, '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL'");
$iBookingID = $oConn->insert_id;
我没有收到任何错误,$ iBookingID为0.该行没有插入到数据库中。我已经阅读了StackExchange上的PHP手册和类似的帖子,但是无法解决这个问题。
答案 0 :(得分:1)
错误 - 你忘了添加单引号和括号。
$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values
('$iProviderID', '$iRatesID', '$sSecret', '$sDate', '$sAltDate', '$iAdults', '$iChildren', '$iTransfer', '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL')");
答案 1 :(得分:1)
... // $_REQUEST variables processed
$oConn = new mysqli("localhost","user","password","mydatabase") or die("Error " . $oConn->error);
$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values
('$iProviderID', '$iRatesID', '$sSecret', '$sDate', '$sAltDate', '$iAdults', '$iChildren', '$iTransfer', '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL')");
$iBookingID = $oConn->insert_id;
echo $iBookingID;
答案 2 :(得分:0)
你的代码上有一个错字,你错过了关闭")"您查询的值部分。
正确的查询:
$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values ($iProviderID, $iRatesID, $sSecret, '$sDate', '$sAltDate', $iAdults, $iChildren, $iTransfer, '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL');");
答案 3 :(得分:0)
如果这不是php错误,您可以尝试使用echo $oConn->error
来获取mysql查询错误!