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 ) );
Discussions

Why is MySQL rejecting a null insert? - Databases - SitePoint Forums | Web Development & Design Community
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 ... More on sitepoint.com
🌐 sitepoint.com
0
May 27, 2019
select - MySQL Insert NULL Inserts 0 - Database Administrators Stack Exchange
I am using MySQL and PhpMyAdmin, I have a table named items - the structure is as follows; +------+---------+------+---------+ | name | type | Null | Default | +------+---------+------+--------... More on dba.stackexchange.com
🌐 dba.stackexchange.com
Insert data in MySQL table
I am trying to insert data into ... are not null: I am using “Execute non query” activity with “Parameters” and “sql” query like this: SQL Query = “Insert into Table_Name values(@Buque_Vessel, @Viaje_Voyage, @E_T_A, @Terminal)” Connection to MySQL data base ... More on forum.uipath.com
🌐 forum.uipath.com
1
0
August 17, 2021
Insert NULL value in SQL from Powershell
The reason what you have isn't working is because you're inserting an empty string into your table, not null. It would be best if you used parameters: $testcol = [System.DBNull]::Value $SQL = "insert into [cadb].[dbo].[testtable] (testcol) values ( @testcol )" Invoke-SQLCmd2 -ServerInstance "randomserver" -Query $SQL -SqlParameters @{testcol = $testcol} More on reddit.com
🌐 r/PowerShell
5
3
October 13, 2020
🌐
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> ...
🌐
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.
🌐
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)?
🌐
PHPBuilder
board.phpbuilder.com › d › 10363548-inserting-null-values-into-mysql-with-php
Inserting Null Values into MySQL with PHP - PHPBuilder Forums
March 6, 2009 - Hello, Hello. I'm having a hell of a time (probably due to lack of sleep and deminished brain capacity, not that I had some to begin with) inserting null va...
🌐
TutorialsPoint
tutorialspoint.com › insert-null-value-into-int-column-in-mysql
Insert NULL value into INT column in MySQL?
June 25, 2020 - 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 ...
Find elsewhere
🌐
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)
🌐
PopSQL
popsql.com › learn-sql › mysql › how-to-add-a-not-null-constraint-in-mysql
MySQL: Not Null Constraint - PopSQL
--Example: Products have a default stock of 0 ALTER TABLE products MODIFY stocks INT NOT NULL; Note that you MUST restate the full column definition, otherwise undeclared attributes will go back to default settings. For example, not restating the DEFAULT clause will unset the default value. To ensure that you do not miss anything, you can use the SHOW CREATE TABLE command to see the full column definition: mysql> SHOW CREATE TABLE products\G *************************** 1.
🌐
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
🌐
Atlassian
atlassian.com › data › admin › how-to-insert-if-row-does-not-exist-upsert-in-mysql
Upsert Techniques in MySQL: INSERT If Not Exists | Atlassian
No existing data row is found with matching values and thus a standard INSERT statement is performed. A matching data row is found, causing that existing row to be deleted with the standard DELETE statement, then a normal INSERT is performed afterward. For example, we can use REPLACE to swap out our existing record of id = 1 of In Search of Lost Time by Marcel Proust with Green Eggs and Ham by Dr. Seuss: mysql> REPLACE INTO books (id, title, author, year_published) VALUES (1, 'Green Eggs and Ham', 'Dr.
🌐
MySQL
bugs.mysql.com › bug.php
MySQL Bugs: #47122: The NULL value in INSERT statements is quoted
To distinguish NULL value from `NULL` literal it has to be set using popup menu - right click on the cell(s) you want to nullify, then select 'Set selection to NULL' menu item. NULL values set this way will export as expected. ... When doing that, I get an empty string in the insert statement.
🌐
SitePoint
sitepoint.com › databases
Why is MySQL rejecting a null insert? - Databases - SitePoint Forums | Web Development & Design Community
May 27, 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.
🌐
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 ...
🌐
Sololearn
sololearn.com › en › Discuss › 2087524 › how-to-insert-blank-value-not-null-using-mysql-query
How to insert blank value not NULL using mysql query | Sololearn: Learn to code for FREE!
December 3, 2019 - If it was a VARCHAR family, you may use '' (adjacent single quotes - an empty string), and if it accepts null you can use NULL (no quotes, all caps).
🌐
MySQL
forums.mysql.com › read.php
MySQL :: How to insert NULL values directly?
June 13, 2005 - How to insert NULL values directly into MySQL Query Browser like I do with CTRL+0 in Microsoft SQL Server Enterprise Manager?
🌐
MySQL
dev.mysql.com › doc › en › insert.html
MySQL 8.4 Reference Manual :: 15.2.7 INSERT Statement
This is 0 for numeric types, the empty string ('') for string types, and the “zero” value for date and time types. INSERT INTO ... SELECT statements are handled the same way as multiple-row inserts because the server does not examine the result set from the SELECT to see whether it returns a single row. (For a single-row INSERT, no warning occurs when NULL is inserted into a NOT NULL column.
🌐
SQLines
sqlines.com › mysql › statements › insert
MySQL - INSERT - Guide, Examples and Alternatives - SQLines Tools
INSERT IGNORE INTO cities VALUES (5, 'San Jose', 'United States'), -- id 5 already exists, this row will be skipped (NULL, 'Lyon', 'France'), -- id 6 will be assigned (NULL, 'Warsaw', 'Poland'); -- id 7