I have a database table that has a bunch of VARCHAR columns for things like 'location', 'occupation', etc, and most of these columns are DEFAULT NULL.
When a user signs up for this application, a record in the above table is created and all the columns are set to DEFAULT NULL, awaiting any future updates when the user feels like adding info to their profile.
There is a simple XHTML form that handles the text field inputs and submits the form to process the MySQL UPDATE with the updated data. Here is an example of how I validate (very simplistic for ease of reading) and UPDATE the record in the database:
Code: Select all
if (isset($_POST['location']) && is_string($_POST['location']))
{
$clean['location'] = escape_data($_POST['location']);
} else
{
$clean['location'] = NULL;
}
$query = "UPDATE table1 SET location='{$clean['location']}' WHERE id=8";
$result = mysql_query($query);Code: Select all
<?php
$infoQuery = "SELECT location FROM table1 WHERE id=8 LIMIT 1";
$infoResult = mysql_query($infoQuery);
$info = mysql_fetch_assoc($infoResult);
?>
<input type="text" name="location" size="100" maxlength="250" value="
<?php
if (isset($_POST['location']) && !empty($_POST['location']))
echo outputThis($_POST['location']);
elseif (!empty($info['location']))
echo outputThis($info['location']); ?>
" />Is there anyway I can change my UPDATE statement to properly insert NULL into the location column if the user submits the form without actually typing anything into the location input field? Thanks for any help on this matter, it is greatly appreciated.