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
🌐
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', ( CASE WHEN '$jobTitle' = '' THEN NULL ELSE '$jobTitle' END ) );
🌐
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
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> ...
🌐
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 − ... mysql> create table insertNullValue -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(100), -> ClientCountryName varchar(20) -> ); Query OK, 0 rows affected (0.54 sec)
🌐
GeeksforGeeks
geeksforgeeks.org › mysql › mysql-handling-null-values
MySQL Handling NULL Values - GeeksforGeeks
July 23, 2025 - When creating tables in MySQL we can specify whether a column allows the NULL values or not.
🌐
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> ...
🌐
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
Find elsewhere
🌐
W3Schools
w3schools.com › mysql › mysql_null_values.asp
MySQL NULL Values - IS NULL and IS NOT NULL
MySQL SQL MySQL SELECT MySQL WHERE MySQL AND, OR, NOT MySQL ORDER BY MySQL INSERT INTO MySQL NULL Values MySQL UPDATE MySQL DELETE MySQL LIMIT MySQL MIN and MAX MySQL COUNT, AVG, SUM MySQL LIKE MySQL Wildcards MySQL IN MySQL BETWEEN MySQL Aliases MySQL Joins MySQL INNER JOIN MySQL LEFT JOIN MySQL RIGHT JOIN MySQL CROSS JOIN MySQL Self Join MySQL UNION MySQL UNION ALL MySQL GROUP BY MySQL HAVING MySQL EXISTS MySQL ANY, ALL MySQL INSERT SELECT MySQL CASE MySQL Null Functions MySQL Comments MySQL Operators
🌐
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'; ...
🌐
Webmaster World
webmasterworld.com › php › 4361321.htm
inserting NULL to mysql db field with a php variable - PHP Server Side Scripting forum at WebmasterWorld - WebmasterWorld
$sql = "UPDATE blabla SET whatever = '".$myvar."', whichever='".$othervar."' WHERE idBla= '".$somenumber.'"; I wonder if I should set $myvar or $othervar to the php NULL value like ... $myvar = "NULL"; and would that be compatible with the mysql NULL value(while enclosing $myvar in single quotes in the query string)?
🌐
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.
🌐
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…
🌐
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

🌐
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
I want to insert a NULL value into my integer column. ... mysqli_query($con, "INSERT INTO Application (startYear) VALUES ('".$startYear."')"); Before you ask, I have checked if $startYear is ever empty, and it is, so this should work.
🌐
SitePoint
sitepoint.com › databases
Why is MySQL rejecting a null insert? - Databases - SitePoint Forums | Web Development & Design Community
May 28, 2019 - New coder here trying to figure out why I am getting an error. I have been coding on Could9.io for several months and had no issues with any of my mysql coding. I am now on Codeanywhere and am getting several errors. I’m guessing they are running different mysql versions, or have different settings enabled.