Skip to content

Can not insert null values. #7504

@jdarwood007

Description

@jdarwood007

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions