473,386 Members | 1,830 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,386 software developers and data experts.

Differentiate between NULL and 0

I have a script which takes the results of a form and updates or creates
a record in a db table. So far so dull. It works fine, except when I
leave certain fields blank; these are entered in the table as zero
values. Can anyone assist me to differentiate between NULL and zero? the
variables that are troubling are fatalities, injuries, latitude and
longitude.

TIA

Garry

code:

# load variables into array
$variables["type"] = $type;
$variables["location"] = $location;
$variables["fatalities"] = $fatalities;
$variables["injuries"] = $injuries;
$variables["latitude"] = $latitude;
$variables["longitude"] = $longitude;
$variables["development"] = $development;
$variables["list"] = $list;
$variables["display"] = $display;
$variables["description"] = $description;

foreach ($variables as $key => $value) {
# loop through variables;
$element =& $variables[$key];
if (isset($value)) {
# if variable is not null, add quotes for sql query
$element = "'" . $element . "'";
} else {
# variable is NULL - no quotes
$element = "NULL";
}
unset($element);
}

# explode array back to variables
extract($variables);

if(isset($eventID)) {
# updating an existing record
# create SQL from variables
$sql = "
UPDATE tblevents
SET enumType = $type,
locationID = $location,
datDate = '$eventDate',
intFatalities = $fatalities,
intInjuries = $injuries,
dblLatitude = $latitude,
dblLongitude = $longitude,
developmentID = $development,
enumList = $list,
enumDisplay = $display,
txtDescription = $description
WHERE eventID = $eventID
";
} else {
# creating a new record
$sql = "
INSERT INTO tblevents (
eventID,
enumType,
locationID,
datDate,
intFatalities,
intInjuries,
dblLatitude,
dblLongitude,
developmentID,
enumList,
enumDisplay,
txtDescription
) VALUES (
NULL,
$type,
$location,
'$eventDate',
$fatalities,
$injuries,
$latitude,
$longitude,
$development,
$list,
$display,
$description
)
";
}

# execute SQL
mysql_query($sql) or die(mysql_error());
Jan 15 '06 #1
4 2026

GazK wrote:
I have a script which takes the results of a form and updates or creates
a record in a db table. So far so dull. It works fine, except when I
leave certain fields blank; these are entered in the table as zero
values. Can anyone assist me to differentiate between NULL and zero? the
variables that are troubling are fatalities, injuries, latitude and
longitude.

TIA

Garry


Zero is a number. NULL has no value assigned.

http://www.askblax.com

Jan 15 '06 #2
askMe wrote:
GazK wrote:
I have a script which takes the results of a form and updates or creates
a record in a db table. So far so dull. It works fine, except when I
leave certain fields blank; these are entered in the table as zero
values. Can anyone assist me to differentiate between NULL and zero? the
variables that are troubling are fatalities, injuries, latitude and
longitude.

TIA

Garry


Zero is a number. NULL has no value assigned.

http://www.askblax.com


Thanks, thats given me the answer; insert a test for
is_numeric($variable) on these items. Thanks again.
Jan 15 '06 #3
On 2006-01-15, GazK <ru*****@rubbish.com> wrote:
Thanks, thats given me the answer; insert a test for
is_numeric($variable) on these items. Thanks again.


if ($variable !== 0) {
// ...
}
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>
Jan 15 '06 #4
The easiest way I have found to test for the existence or non-existence of a
value in the $_POST array, and to avoid any confusion between empty, null
and zero, is to use the strlen() function. If the result is a length of
zero, then it signifies no input. Easy peasy lemon squeezy.

--
Tony Marston

http://www.tonymarston.net

"GazK" <ru*****@rubbish.com> wrote in message
news:4M********************@eclipse.net.uk...
I have a script which takes the results of a form and updates or creates a
record in a db table. So far so dull. It works fine, except when I leave
certain fields blank; these are entered in the table as zero values. Can
anyone assist me to differentiate between NULL and zero? the variables that
are troubling are fatalities, injuries, latitude and longitude.

TIA

Garry

code:

# load variables into array
$variables["type"] = $type;
$variables["location"] = $location;
$variables["fatalities"] = $fatalities;
$variables["injuries"] = $injuries;
$variables["latitude"] = $latitude;
$variables["longitude"] = $longitude;
$variables["development"] = $development;
$variables["list"] = $list;
$variables["display"] = $display;
$variables["description"] = $description;

foreach ($variables as $key => $value) {
# loop through variables;
$element =& $variables[$key];
if (isset($value)) {
# if variable is not null, add quotes for sql query
$element = "'" . $element . "'";
} else {
# variable is NULL - no quotes
$element = "NULL";
}
unset($element);
}

# explode array back to variables
extract($variables);

if(isset($eventID)) {
# updating an existing record
# create SQL from variables
$sql = "
UPDATE tblevents
SET enumType = $type,
locationID = $location,
datDate = '$eventDate',
intFatalities = $fatalities,
intInjuries = $injuries,
dblLatitude = $latitude,
dblLongitude = $longitude,
developmentID = $development,
enumList = $list,
enumDisplay = $display,
txtDescription = $description
WHERE eventID = $eventID
";
} else {
# creating a new record
$sql = "
INSERT INTO tblevents (
eventID,
enumType,
locationID,
datDate,
intFatalities,
intInjuries,
dblLatitude,
dblLongitude,
developmentID,
enumList,
enumDisplay,
txtDescription
) VALUES (
NULL,
$type,
$location,
'$eventDate',
$fatalities,
$injuries,
$latitude,
$longitude,
$development,
$list,
$display,
$description
)
";
}

# execute SQL
mysql_query($sql) or die(mysql_error());

Jan 15 '06 #5

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

Similar topics

0
by: Chung Leong | last post by:
Q. How do I differentiate an empty string or numeric zero from false? A. Use the === operator.
1
by: Suresh | last post by:
Hi all, Here is my code, if(event.which) keycode = event.which;//For Mozilla else keycode = event.keyCode;//For IE But my problem is, the value of keycode returns 46 for pressing...
5
by: mayur_hirpara | last post by:
Hi, I have been developing web applications for a while now. However, as I was thinking through the architecture I really don't understand the "How server can identify between which buttons has...
2
by: adam | last post by:
Hi ASP Expert, I encounter a page reload situation in ASP. It is I need a way to differentiate whether the current page - "Application_Result.asp" got reloaded itself when user click on the...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
3
by: mayur_hirpara | last post by:
Hi, I am writing a VB.NET application. It is a Windows application at the moment. The application contains a button. the click event of button perform a long running task. It needs to be started...
1
dmjpro
by: dmjpro | last post by:
plz differentiate between JRE ,JDK and SDK ...... thanxxxx
1
by: odavilar | last post by:
hi, i'm trying to do this: string x; cin>>x; if (x is one word){ something }else{
2
by: Hans-Joerg Unger | last post by:
Hi ! How can I differentiate nodes in an xml-schema which have the same name (see "file"-element in the example) , but different locations and meanings in the xml tree: How can i...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...

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.