|
Posted by Paul C. Jackson on 09/17/05 01:06
Dave Thomas wrote:
> If I have a table set up like this:
>
> Name | VARCHAR
> Email | VARCHAR
> Age | TINYINT | NULL (Default: NULL)
>
> And I want the user to enter his or her name, email, and age - but AGE
> is optional.
>
> My insert would look something like:
>
> INSERT INTO data (Name, Email, Age) VALUES ('$name', '$email', $age)
>
> This is all good, except if the user doesn't enter an age. Then $age is
> an empty variable. I thought that since the table is set up where Age
> can be NULL, that this should be fine. But MySQL is giving me an error.
> If $age is a number, it is no problem.
>
> Anyway to make it accept $age as an empty variable (and just make it
> NULL)? I know I could say:
>
> if (empty($age)) { $age = 0; }
>
> and write it that way, but this is just an example and there are many
> more variables that could be empty.
>
> Thanks.
I do it this way:
if (isset($_POST["Cases"])){
if(isset($debug)) print "<!-- checking for submition of post data
-->\n";
if((isset($_POST["submit"]) and
($_POST["submit"] == "insert"))) {
if(isset($debug)) print "<!-- submit is set -->\n";
$query =" INSERT INTO jobs SET";
if(isset($_POST["PatientLastName"]))
$query = $query." PtNameLast = \"".
mysql_escape_string($_POST["PatientLastName"])."\", ";
if(isset($_POST["PatientFirstName"]))
$query = $query." PtNameFirst = \"".
mysql_escape_string($_POST["PatientFirstName"])."\", ";
if(isset($_POST["Prescriber"]))
$query = $query." Prescriber = ".$_POST["Prescriber"].", ";
if(isset($_POST["office"]))
$query = $query." OriginatingOffice = ".$_POST["office"].", ";
if(isset($_POST["DueDate"]))
$query = $query."DueDate = \"".
mysql_escape_string($_POST["DueDate"])."\" ,";
if(isset($_POST["arrivialDate"]))
$query = $query."arrivialDate = \"".
mysql_escape_string($_POST["arrivialDate"])."\", ";
if(isset($_POST["RxDate"]))
$query = $query."RxDate = \"".
mysql_escape_string($_POST["RxDate"])."\",";
if(isset($_POST["WOnumber"]))
$query = $query." WOnumber = \"".
mysql_escape_string($_POST["WOnumber"])."\"";
if(isset($debug)) print "<!-- $query -->\n";
$result = mysql_query($query) or die("Query failed");
}
if(isset($debug)) print "<!-- finished with submitted processing
-->\n";
that way if anything is missing it doesn't bomb out the whole thing, or
make it bomb out the wjole page and send back an error to the user if
something absolutely necessary is missing
Navigation:
[Reply to this message]
|