我有以下PHP代码:
$query = array(
$this->userid_to,
$userid_to,
$this->gibid,
$this->card_type,
$message,
$isDeck
);
print_r($query);
$sql = "INSERT INTO
cards (
userid_from,
userid_to,
gibid,
card_type,
message,
isDeck,
createdon
) VALUES (?,?,?,?,?,?,current_timestamp)";
Db::execute($sql,$query);
$this->cardid=Db::getLastInsertId();
在运行此操作时,我将$ message变量作为空白传递,因此我收到此错误:
Array (
[0] => 1
[1] => 4
[2] => 0
[3] => V
[4] =>
[5] => N )
Fatal error: Uncaught exception
'PDOException' with message 'Database
error [23000]: Column 'message' cannot
be null, driver error code is 1048' in
/var/www/ryapi/Db.php:179 Stack trace:
0 /var/www/ryapi/Db.php(54): Db::_query('INSERT INTO car...',
Array)
1 /var/www/ryapi/card.php(79): Db::execute('INSERT INTO car...',
Array)
2 /var/www/ryapi/index.php(173): Card->givecard('4', NULL, NULL)
3 {main} thrown in /var/www/ryapi/Db.php on line 179
如果我想将$message
作为空白,我该如何删除此错误。
答案 0 :(得分:1)
我认为这个列不能是(null)。检查是否可以使列可以为空。
(null)和空字符串不是“值”。
答案 1 :(得分:0)
确保$message
实际上是一个空字符串,而不是值NULL
。
答案 2 :(得分:0)
两种解决方案 - 如果你真的希望消息值为“null”,那么在数据库中使“message”列可以为空。如果它打破了应用程序的其他部分,您需要小心这样做。 - 传递像空格一样的虚拟值 - 即“”OR字符串如“none”或任何表示确实没有消息的字符串