SMF
SMF copied to clipboard
Can not insert null values.
Description
Lets assume you have a table that allows nulls
CREATE TABLE `test` ( `id` INT NOT NULL AUTO_INCREMENT , `mycol` INT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
Try to use a $smcFunc['insert'] on this.
$smcFunc['db_insert']('insert',
'test',
[
'id' => 'int',
'mycol' => int'
],
[
[
'id' => 1,
'mycol' => null
]
]
);
This will fail with a error The database value you're trying to insert does not exist: mycol
This is because of this line of code
if (!isset($values[$matches[2]]))
smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__);
PHP returns false for isset on nulls. A way around this is to use in_array($matches[2], $values). This will return true.
array_key_exists
no need to waste cycles searching the array
Sorry, array_key_exists, not in array.