|
Posted by GazK on 01/15/06 14:15
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());
Navigation:
[Reply to this message]
|