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_dat e,
application_tim e, referring_web_s ite, keywords, first_name, last_name,
email, address, city, state, zip, home_phone, business_phone,
best_time_to_ca ll)
values ('', '$appDate', '$appTime', '$Campaign', '$kw', '$fFirstName',
'$fLastName', '$fEmail', '$fAddress', '', '$fState', '$fZip',
'$fHomePhoneCom bined', '$fBusinessPhon eCombined', '$fBestTime')";
$result = mysql_query($qu ery) or die('Failed because:
'.mysql_error() );
An example of one of the fields that may need to be null is
$fBusinessPhone Combined. 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 ($fBusinessPhon e1 == ""){
$fBusinessPhone Combined = '';
}
if ($fBusinessPhon e1 == ""){
$fBusinessPhone Combined = NULL;
}
if ($fBusinessPhon e1 == ""){
$fBusinessPhone Combined = 'NULL';
}
HELP! :-)
Thanks in advance,
- Mark 2 58355 da**********@ho tmail.com (Mark Davenport) wrote in
news:3b******** *************** ***@posting.goo gle.com: values ('', '$appDate', '$appTime', '$Campaign', '$kw', '$fFirstName', '$fLastName', '$fEmail', '$fAddress', '', '$fState', '$fZip', '$fHomePhoneCom bined', '$fBusinessPhon eCombined', '$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_st ring($col1) . "'";
} else {
$col1 = "NULL"; // in the SQL query "NULL" will NOT be quoted
}
It is always a good idea to use mysql_escape_st ring 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
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_dat e, application_tim e, referring_web_s ite, keywords, first_name, last_name, email, address, city, state, zip, home_phone, business_phone, best_time_to_ca ll) values ('', '$appDate', '$appTime', '$Campaign', '$kw', '$fFirstName', '$fLastName', '$fEmail', '$fAddress', '', '$fState', '$fZip', '$fHomePhoneCom bined', '$fBusinessPhon eCombined', '$fBestTime')";
"INSERT INTO customers SET application_dat e='$appDate', application_tim e='$appTime', referring_web_s ite='$Campaign' , keywords='$kw', first_name='$fF irstName', last_name='$fLa stName', email='$fEmail' , address='$fAddr ess', state='$fState' , zip='$fZip', home_phone='$fH omePhoneCombine d', business_phone= '$fBusinessPhon eCombined', best_time_to_ca ll='$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 $fBusinessPhone Combined. 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_dat e='$appDate', application_tim e='$appTime', referring_web_s ite='$Campaign' , keywords='$kw'" ;
if(!empty(trim( $fBusinessPhone 1)) $q.=", business_phone= '$BusinessPhone 1'";
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 is left
blank is NOT inserted as a NULL value into the DB as I require but as blank
data.
How can I modify the DW-created statement below to insert a NULL if the
field is empty?
|
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 null
values.
¿What do i have to write in the SQL sentence to insert a null value in
those fields?
I tried with '', an empty String, but it doesnt work, the tables
stores an empty String (logical :-)).
In the SQLServer GUI you have to press CTRL +...
|
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 string or NULL.
Becaus i have a if statement:
if ....
cursor.execute(".................insert NULL ..............")
if ....
|
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 Field!
(It is successful to pass the Null Value to other type of
Field)
|
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 place...can anyone help me??
i didnt made anything in the VALUES (unless the POST ), because i bet the error is there (yes, the arrays are missing):
$sql = "INSERT INTO dgplanets ( ID, coords , player name , alliance , name , ground , orbit ,...
| |
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
field. The pymssql._quote() puts ' around the NULL which causes an
exception to be thrown, is there a way to use the params for this or
do I need to build the insert string myself?
pymssql.DatabaseError: internal error: SQL Server message 245,...
|
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 users select all
Checkbox1
Checkbox2
Checbox3
Table structure:
Date, checknames
|
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 MySQL Insert
statement. I am familiar with PHP, where you would write
$_POST(), however I can not find the equivalent in PSP. I
believe that this is the my missing piece. Technically, what is
occurring that that when I click the 'Submit'...
|
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 <CPU>Intel</CPU> node below) but it throws an Exception error when the node value is blank/null whatever as in the example node <Monitor> </Monitor>.
It's ok if the node value is blank, I just want to check for it and not throw an exception...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |