How can I insert a NULL
value into a database field (MySQL) with char(2)?
Before I used mysql_query
and I got in the database the following value: NU
Instead of this value there should be NULL
.
After reading this question I switched to prepared statements. I still have NU
in my database.
Now to my code. I'm reading out line for line of a CSV. Then I make
if(empty($data[$c])) {
$data[$c] = NULL;
}
If I echo $data[1] I get NULL
. Then I make my prepared query:
$stmt = $connection->prepare("INSERT INTO table (first, second, ..) VALUES (?, ?, ...)");
$stmt->bind_param("ss...", $data[0], $data[1], ...);
$res=$stmt->execute();
What is wrong?
Edit:
According to Shiplu I did the following:
Do it without giving the last params which causes NU
:
$stmt = $connection->prepare("INSERT INTO employee (first, second, ...) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssssssssii", $data[0], $data[1], ..., $last);
Using default
:
$stmt = $connection->prepare("INSERT INTO employee (first, second, ..., last) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, default(last))");
$stmt->bind_param("sssssssssssssii", $data[0], $data[1], ...);
But still the same result. The char(2) field has default = NULL. How can that be? It seems that the problem is not with the query. I think the easiest solution would be an update query ...
Create table:
CREATE TABLE IF NOT EXISTS `mytable` (
`id` int(11) NOT NULL auto_increment,
`text` varchar(128) default NULL,
`no` int(11) default NULL,
`problemfield` char(2) default NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `text` (`text`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=832 ;