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()); 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
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.
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>
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()); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chung Leong |
last post by:
Q. How do I differentiate an empty string or numeric zero from false?
A. Use the === operator.
|
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...
|
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...
|
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...
|
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;
|
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...
|
by: dmjpro |
last post by:
plz differentiate between JRE ,JDK and SDK ......
thanxxxx
|
by: odavilar |
last post by:
hi, i'm trying to do this:
string x;
cin>>x;
if (x is one word){
something
}else{
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |