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?
INSERT INTO yourTableName(yourColumnName) values(NULL); To understand the above syntax, let us first create a table. The query to create a table is as follows. mysql> create table InsertNullDemo -> ( -> StudentId int, -> StudentName varchar(100), -> StudentAge int -> ); Query OK, 0 rows affected ...
🌐
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 ('');...
🌐
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.
🌐
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.
🌐
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

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> ...
🌐
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.
🌐
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'; ...
🌐
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…
🌐
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 −
🌐
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 ...
🌐
Stack Overflow
stackoverflow.com › questions › 28332965 › how-to-insert-null-value-into-a-column-which-already-have-a-value-using-mysql
sql - How to Insert Null Value into a Column which already have a Value using MySQL - Stack Overflow
UPDATE your_table SET serialNumber = null WHERE ID = 1; ... I am doing insert with a flag to the statement of "update if exists" so I am not calling directly to update statement. ... You can't Insert a value into a column which already have a value.
🌐
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 ...
🌐
YouTube
youtube.com › united top tech
How to insert NULL and empty values in SQL table - YouTube
How to insert null and empty or blank values in SQL is shown
Published   November 25, 2021
Views   18K
🌐
GeeksforGeeks
geeksforgeeks.org › mysql › mysql-handling-null-values
MySQL Handling NULL Values - GeeksforGeeks
July 23, 2025 - INSERT INTO employees (name, salary) VALUES ('Alice', NULL); INSERT INTO employees (name, salary) VALUES (NULL, 50000.00); To handle NULL values in queries we must use specific SQL functions and operators.