473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Insert NULL not Blank into MySQL with PHP Script

Hi,

Here's my question: How do I pass a NULL value in a variable to a
MySQL DB?

Here's an overview of my problem: I have a PHP page that processes
code, then inserts the code into a database. Very straightforward.
But, some NULL values are being inserted as a blank space, or the
string "NULL" instead of a true NULL.

Below is the db insert...

$db = mysql_connect ("localhost", "user name", "password");
mysql_select_db ("database name");

$query = "insert into customers (customer_id, application_date,
application_time, referring_web_site, keywords, first_name, last_name,
email, address, city, state, zip, home_phone, business_phone,
best_time_to_call)
values ('', '$appDate', '$appTime', '$Campaign', '$kw', '$fFirstName',
'$fLastName', '$fEmail', '$fAddress', '', '$fState', '$fZip',
'$fHomePhoneCombined', '$fBusinessPhoneCombined', '$fBestTime')";

$result = mysql_query($query) or die('Failed because:
'.mysql_error());
An example of one of the fields that may need to be null is
$fBusinessPhoneCombined. If the user does not enter a telephone number
into the business phone field, then I would like to insert a NULL
value into the database. I tried the following code (at different
times) to make the variable pass a null value, but these don't work. I
either get a blank entry, or the string "NULL" inserted into the DB:

if ($fBusinessPhone1 == ""){
$fBusinessPhoneCombined = '';
}
if ($fBusinessPhone1 == ""){
$fBusinessPhoneCombined = NULL;
}

if ($fBusinessPhone1 == ""){
$fBusinessPhoneCombined = 'NULL';
}
HELP! :-)
Thanks in advance,
- Mark
Jul 16 '05 #1
2 58279
da**********@hotmail.com (Mark Davenport) wrote in
news:3b**************************@posting.google.c om:
values ('', '$appDate', '$appTime', '$Campaign', '$kw', '$fFirstName',
'$fLastName', '$fEmail', '$fAddress', '', '$fState', '$fZip',
'$fHomePhoneCombined', '$fBusinessPhoneCombined', '$fBestTime')";


Don't use quotes if you want to insert NULL values. Instead write:

values ($col1, $col2, $col3 .......

Then add quotes to variables that have a value and set all other to be
"NULL":

if ($col1) {
$col1 = "'" . mysql_escape_string($col1) . "'";
} else {
$col1 = "NULL"; // in the SQL query "NULL" will NOT be quoted
}

It is always a good idea to use mysql_escape_string but not really
necessary to solve this problem.
Hope this helps,
Udo

--
To reply by e-mail, use following address:
udonews AT nova-sys.net
Jul 16 '05 #2
Justin Koivisto <sp**@koivi.com> wrote in message news:<RB***************@news7.onvoy.net>...
Mark Davenport wrote:
Here's an overview of my problem: I have a PHP page that processes
code, then inserts the code into a database. Very straightforward.
But, some NULL values are being inserted as a blank space, or the
string "NULL" instead of a true NULL.
first, be sure the field doesn't specify NOT NULL.


Yep, I checked that. Thanks for mentioning it though! :-)

$db = mysql_connect ("localhost", "user name", "password");
mysql_select_db ("database name");

$query = "insert into customers (customer_id, application_date,
application_time, referring_web_site, keywords, first_name, last_name,
email, address, city, state, zip, home_phone, business_phone,
best_time_to_call)
values ('', '$appDate', '$appTime', '$Campaign', '$kw', '$fFirstName',
'$fLastName', '$fEmail', '$fAddress', '', '$fState', '$fZip',
'$fHomePhoneCombined', '$fBusinessPhoneCombined', '$fBestTime')";


"INSERT INTO customers SET application_date='$appDate',
application_time='$appTime', referring_web_site='$Campaign',
keywords='$kw', first_name='$fFirstName', last_name='$fLastName',
email='$fEmail', address='$fAddress', state='$fState', zip='$fZip',
home_phone='$fHomePhoneCombined',
business_phone='$fBusinessPhoneCombined', best_time_to_call='$fBestTime';

Notice that customer_id and city where not included in the statment. By
doing this, MySQL will assume that the value is NULL.
An example of one of the fields that may need to be null is
$fBusinessPhoneCombined. If the user does not enter a telephone number
into the business phone field, then I would like to insert a NULL
value into the database. I tried the following code (at different
times) to make the variable pass a null value, but these don't work. I
either get a blank entry, or the string "NULL" inserted into the DB:


Construct your query as you decide what should be included....

$q="INSERT INTO customers SET application_date='$appDate',
application_time='$appTime', referring_web_site='$Campaign',
keywords='$kw'";

if(!empty(trim($fBusinessPhone1))
$q.=", business_phone='$BusinessPhone1'";

Repeat the if statement for each of the fields that would possibly be
NULL. Then at the end, execute the query.

HTH

This worked perfectly! Thanks Justin. The next beer's on me! :-)

Take care,
- Mark
Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Keith | last post by:
I am Using Dreamweaver MX to create my site and have come accross a problem no one in the DW groups seems to be able to help with. When I submit an insert to my SQL database, any form value which...
2
by: schumacker | last post by:
Hi everyone! I am working with Delphi v7 and MS SQLServer. I am trying to insert data in a table with a SQL sentence. Some of the fields of my table are type char or varchar, and they can have...
10
by: Python_it | last post by:
Python 2.4 MySQL-python.exe-1.2.0.win32-py2.4.zip How can I insert a NULL value in a table (MySQL-database). I can't set a var to NULL? Or is there a other possibility? My var must be variable...
6
by: FatboyCanteen | last post by:
When I using dataset to append a null value to the datetime field. It throw a error -> can not convert db.null to system.date Can there is any standard to pass a Null value to the DateTime...
2
by: blitzztriger | last post by:
Hello!! how do i insert values into mysql , after parsing a submiting textbox?? I made the arrays, so this should be a basic insertion of them in the db, but something is missing, or in the wrong...
3
by: rc | last post by:
How to insert NULL values in to int field using params. I'm trying to use pymssql.execute, passing the operation and list of params. One of the values in the params is a NULL value going to int...
5
ddtpmyra
by: ddtpmyra | last post by:
I don’t know if I posted my question on the right forum if not my bad, but I’m having trouble how to create a php script that will insert data on mysql Scenario: I have 3 checkbox on my form and...
0
by: brianrpsgt1 | last post by:
I am attempting to insert data from a HTML form using a .psp script. I can not find how to link the data that is inserted into the form to the variables in the .psp script to then insert into the...
1
by: jrlittle86 | last post by:
I know I'm overlooking something very simple but How can I check if the text within an XML tag is blank? The code below works fine as long as there are values within an XML node (For example the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.