SMF icon indicating copy to clipboard operation
SMF copied to clipboard

Can not insert null values.

Open jdarwood007 opened this issue 2 years ago • 2 comments

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.

jdarwood007 avatar Jun 14 '22 23:06 jdarwood007

array_key_exists

no need to waste cycles searching the array

live627 avatar Jun 15 '22 06:06 live627

Sorry, array_key_exists, not in array.

jdarwood007 avatar Jun 15 '22 13:06 jdarwood007