无法插入到 mysql 表中 - mysqli 错误不起作用


Cannot insert into mysql table - mysqli error not working

我正在尝试将一些数据插入 mysql 表中,但该过程没有通过,并且我没有得到任何针对该错误打印的内容。我认为我没有正确输出错误。看着 php.net 似乎我正确地进行了错误处理,但也许我错过了一些东西。

这是代码

$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$sqlInsert = $db->query("INSERT INTO transactions(`user_id`, `courses`, `invoice`, `start_date`, `end_date`, `mc_gross`, `email`) VALUES ('".$user_id."','".$trim."','".$invoice."','".$start_date."','".$end_date."', '".$email."')");

if($sqlInsert)
    {
    echo "Inserted successfully";
    }
    else
      {
       printf("Error: ", $db->error);
       }

变量的值如下所示

$custom = $_SESSION['custom'];
$user_id = $_POST['userID'];
$pwd = $_POST['password'];
$email = $_POST['email'];
$start_date = date('Y-m-d');
$end_date = date (('Y-m-d'), strtotime('+120 days'));
$invoice = date('Ymd'). mt_rand(1252,10000);
$invoice_check = $db->query("SELECT `invoice` FROM `transactions` WHERE `invoice` = $invoice");
while ($rows = $invoice_check->fetch_assoc())
{
    $invoice = date('Ymd'). mt_rand(1252,10000);   
}
$mc_gross = $_SESSION['subtotal'];
$trim = rtrim($custom, ",");

$trim的var_dump是string(17) "bio_01,calculus_01",其他变量只是像您期望的那样正常回显。

有什么想法吗?

编辑 使用 $db 而不是 $sqlInsert 更新了代码。仍然没有错误的输出。

更改:

printf("Error: ", $sqlInsert->error);

自:

printf("Error: %s", $db->error);

你不能读取falseerror属性,因为它不是一个对象。而且您缺少printf格式字符串中用于替换参数的%s