This is one example where using prepared statements really saves you some trouble.

In MySQL, in order to insert a null value, you must specify it at INSERT time or leave the field out which requires additional branching:

INSERT INTO table2 (f1, f2)
  VALUES ('String Value', NULL);

However, if you want to insert a value in that field, you must now branch your code to add the single quotes:

INSERT INTO table2 (f1, f2)
  VALUES ('String Value', 'String Value');

Prepared statements automatically do that for you. They know the difference between string(0) "" and null and write your query appropriately:

$stmt = $mysqli->prepare("INSERT INTO table2 (f1, f2) VALUES (?, ?)");
$stmt->bind_param('ss', $field1, $field2);

$field1 = "String Value";
$field2 = null;

$stmt->execute();

It escapes your fields for you, makes sure that you don't forget to bind a parameter. There is no reason to stay with the mysql extension. Use mysqli and it's prepared statements instead. You'll save yourself a world of pain.

Answer from Andrew Moore on Stack Overflow
๐ŸŒ
w3resource
w3resource.com โ€บ sql โ€บ insert-statement โ€บ insert-null.php
Sql inserting NULL values - w3resource
-- INSERT INTO statement begins INSERT INTO agents -- Specifies the table 'agents' where the data will be inserted VALUES ("A001","Jodi","London",.12,NULL); -- Specifies the values to be inserted into each column of the table: -- "A001" will be inserted into the 'agent_code' column (assuming it's a string data type) -- "Jodi" will be inserted into the 'agent_name' column -- "London" will be inserted into the 'working_area' column -- .12 will be inserted into the 'commission' column (assuming it's a numeric data type) -- NULL will be inserted into the 'salary' column, assuming it allows NULL values
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ insert-null-value-into-int-column-in-mysql
Insert NULL value into INT column in MySQL?
You can insert NULL value into an int column with a condition i.e. the column must not have NOT NULL constraints. The syntax is as follows. INSERT INTO yourTableName(yourColumnName) values(NULL); To under
๐ŸŒ
Designcise
designcise.com โ€บ web โ€บ tutorial โ€บ how-to-replace-an-empty-string-with-null-in-mysql-insert-query
Replacing Empty String With NULL in MySQL Insert Query - Designcise
September 4, 2020 - INSERT INTO `user` (`name`, `job_title`) VALUES ('john', IF('$jobTitle' = '', NULL, '$jobTitle')); Using CASE could be another option. However, it may not be the best choice when you only have one condition.
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ refman โ€บ 8.4 โ€บ en โ€บ problems-with-null.html
MySQL :: MySQL 8.4 Reference Manual :: B.3.4.3 Problems with NULL Values
MySQL 8.4 Reference Manual / ... ... ''. This is not the case. For example, the following statements are completely different: mysql> INSERT INTO my_table (phone) VALUES (NULL); mysql> INSERT INTO my_table (phone) VALUES ('');...
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ insert null instead of blank if form fields left empty
r/PHPhelp on Reddit: Insert NULL instead of blank if form fields left empty
July 14, 2021 -

I am trying to insert my form data into mysql table there are two required fields and others can be left empty but the problem is the empty fields are inserting blank in sql table instead of NULL value. How do i do this? Here is pastebin code link

๐ŸŒ
Dirask
dirask.com โ€บ posts โ€บ MySQL-Insert-NULL-values-DNKNOp
MySQL - Insert NULL values
CREATE TABLE `users` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(50), `surname` VARCHAR(50), `department_id` INT(10), `salary` DECIMAL(15,2), PRIMARY KEY (`id`) ); ... INSERT INTO `users` ( `name`, `surname`, `department_id`, `salary`) VALUES ('John', 'Stewart', 1, '6000'), ('Chris', 'Brown', 2, '6000'), ('Kate', 'Lewis', 3, '4000'); ... Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks! ... Join to our subscribers to be up to date with content, news and offers.
Find elsewhere
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ refman โ€บ 8.0 โ€บ en โ€บ problems-with-null.html
MySQL :: MySQL 8.0 Reference Manual :: B.3.4.3 Problems with NULL Values
The concept of the NULL value is a common source of confusion for newcomers to SQL, who often think that NULL is the same thing as an empty string ''. This is not the case. For example, the following statements are completely different: mysql> INSERT INTO my_table (phone) VALUES (NULL); mysql> ...
๐ŸŒ
Xojo Programming Forum
forum.xojo.com โ€บ databases
MySQL: How to assign Null Value to Field when Inserting a record? - Databases - Xojo Programming Forum
February 22, 2017 - I am using MySQL. How do I assign a Null value to a CHAR Field when inserting a record? Dim Row As New DatabaseRecord Row.Column("ID") = "ABC" Row.Column("Contents") = Nil mDB.InsertRecord("ABC", Row) Iโ€ฆ
๐ŸŒ
Coursesweb
coursesweb.net โ€บ php-mysql โ€บ insert-select-update-null-value_t
Insert, Select and Update NULL value in MySQL
$sql = "INSERT INTO `table_name` (`column1`, `column2`, `column3`) VALUES ('val1', 'NULL', 'val3')"; The same works with UPDATE. Example: $sql = "UPDATE `table_name` SET `column1`='val1', `column2`=NULL, WHERE `column3`='val3'"; Or: $nul = 'NULL'; ...
๐ŸŒ
Learnversia
learnversia.com โ€บ home โ€บ how to insert null in mysql
How to insert null in MySQL - MySQL Tutorials
February 28, 2024 - Here we will see how we can insert NULL in the MySQL table. Sometimes we donโ€™t have to put anything in some columns while inserting data in MySQL, in that case, we have to insert null to those columns. To insert a null value simply put null in the place of the value.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-do-i-insert-a-null-value-in-mysql
How do I insert a NULL value in MySQL?
To insert a NULL value, you can use UPDATE command. Following is the syntax โˆ’ UPDATE yourTableName SET yourColumnName=NULL; Let us first create a table โˆ’
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sql โ€บ how-to-insert-rows-with-null-values-in-sql
How to Insert Rows with NULL Values in SQL? - GeeksforGeeks
July 23, 2025 - INSERT INTO WORKER VALUES('SAM','ONTARIO',NULL); INSERT INTO WORKER VALUES('TIM',NULL,56); INSERT INTO WORKER VALUES(NULL,'CAIRO',43); INSERT INTO WORKER VALUES(NULL,'MUMBAI',NULL); INSERT INTO WORKER VALUES(NULL,NULL,NULL);
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ mysql-tutorial-excerpt โ€บ 8.0 โ€บ en โ€บ working-with-null.html
MySQL :: MySQL Tutorial :: 4.4.6 Working with NULL Values
Two NULL values are regarded as equal in a GROUP BY. When doing an ORDER BY, NULL values are presented first if you do ORDER BY ... ASC and last if you do ORDER BY ... DESC. A common error when working with NULL is to assume that it is not possible to insert a zero or an empty string into a column defined as NOT NULL, but this is not the case.
๐ŸŒ
MySQL Tutorial
mysqltutorial.org โ€บ home โ€บ mysql basics โ€บ mysql null: the beginnerโ€™s guide
MySQL NULL
December 29, 2023 - To set the value of a column to NULL, you use the assignment operator ( =). For example, to update the phone of David William to NULL, you use the following UPDATE statement: UPDATE leads SET phone = NULL WHERE id = 3;Code language: SQL (Structured ...
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ web-development โ€บ threads โ€บ 485363 โ€บ mysql-inserting-null-value-into-int-column
php - MySQL Inserting NULL Value into INT Column [SOLVED] | DaniWeb
A robust fix is to use a prepared statement and bind the parameter. Bind NULL when the input is empty, otherwise bind the integer. This avoids string coercion and quoting issues entirely: // Normalize input: empty string -> NULL, otherwise cast ...
๐ŸŒ
MySQL
forums.mysql.com โ€บ read.php
How to Insert NULL value into SQL - Here THE Solution
Forum for companies looking to hire MySQL talent. ... Forum for New Users of MySQL Workbench. ... Forum for MySQL Shell. ... Forum for MySQL and Perl. ... Forum for MySQL and PHP. ... Forum for MySQL and Delphi. ... Forum for InnoDB Storage Engine.