0

我对 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 手册和类似帖子,但无法解决此问题。

4

4 回答 4

1

错字-u 忘记添加单引号和括号。

$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')");
于 2013-11-13T12:43:06.897 回答
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;
于 2013-11-13T13:02:36.317 回答
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');");
于 2013-11-13T12:42:49.560 回答
0

如果这不是 php 错误,可以尝试使用echo $oConn->error获取 mysql 查询错误!

于 2013-11-13T12:45:16.387 回答