无法插入mysql表 - mysqli错误无法正常工作

时间:2013-11-05 01:09:17

标签: php mysqli

我正在尝试将一些数据插入到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更新代码。仍然没有输出错误。

1 个答案:

答案 0 :(得分:1)

变化:

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

为:

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

您无法读取error的{​​{1}}属性,因为它不是对象。而且你错过了false格式字符串中的%s来替换参数。

相关问题