Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,676 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,200 people online right now. Registration is fast and FREE... Join Now!




html forms, empty optional fields and NULL

 
Reply to this topicStart new topic

html forms, empty optional fields and NULL

jlrp5
post 26 Jun, 2007 - 12:22 AM
Post #1


New D.I.C Head

*
Joined: 25 Jun, 2007
Posts: 2


My Contributions


hi, i am working on patient data forms for clinic visits. I devised an HTML form which input into a mysql database using php. Some parameters like blood pressure is set as integer variables in the database, set as NULL, meaning they can (and should) be empty if no data is input. I have searched the net and found out how html forms pass POST data as strings which cause problems for numerical type variables, thus ending up as '0' instead of NULL. If i use phpmyadmin, there is no problem since there is a null checkbox for fields that can be NULL, and the data is correctly input as 'empty' not '0'. But i want a friendlier interface than phpmyadmin for medical students and medical staff to use, thus... the need for html form.

So... the question is, can anyone help me to either:
1. come up with HTML/PHP code to place a null checkbox for the html form? OR
2. come up with HTML/PHP code to ignore form fields that are left blank?

this is the "insert" php code:

CODE

<?
$database="stroke";
$table="eclinic";
$NRIC=$_POST['NRIC'];
$TCUDATE=$_POST['TCUDATE'];
$BESTSYS=$_POST['BESTSYS'];
$BESTDIAS=$_POST['BESTDIAS'];
$HR=$_POST['HR'];
$Weight=$_POST['Weight'];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query ="INSERT INTO `eclinic` SET `NRIC`='$NRIC' , `TCUDATE`='$TCUDATE' , `BESTSYS`='$BESTSYS' , `BESTDIAS`='$BESTDIAS' , `HR`='$HR', `Weight`='$Weight' ";
mysql_query($query);
mysql_close();
?>

This is just a very abbreviated version as there are many other variables (blood glucose, cholesterol levels etc). The variable to focus on is Weight. It is "NULL"-able (accepts NULL). Previous attempts of mine using HTML forms always gives me a zero.

We don't always get the patient's weight (aside from the several variables I have left out), so it would be left blank from time to time... but I do not want zeroes where the data ought to be empty/ NULL.

Thanks in advance!
User is offlineProfile CardPM

Go to the top of the page

Styx
post 26 Jun, 2007 - 02:08 AM
Post #2


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


You can check if the field is empty and set the variable to a null string such as:
$weight = (!empty($_POST['weight'])) ? $_POST['weight'] : 'NULL';

In your query, for all the fields that are integer types, take off the single quotations. That's for strings. If an integer is being passed, it will still work. But when you try passing a variable set as a null string, it tries to input it as a string, and since it's an int type, it fails.

Also, more often than not, you wont need the backticks ` since that's just there to ensure they don't conflict with reserved names.

And I hope this is a pretty simplified example, since you don't show any checking on your variables and are open to sql injection.
User is offlineProfile CardPM

Go to the top of the page

jlrp5
post 26 Jun, 2007 - 03:05 AM
Post #3


New D.I.C Head

*
Joined: 25 Jun, 2007
Posts: 2


My Contributions


QUOTE(Styx @ 26 Jun, 2007 - 03:08 AM) *

You can check if the field is empty and set the variable to a null string such as:
$weight = (!empty($_POST['weight'])) ? $_POST['weight'] : 'NULL';

In your query, for all the fields that are integer types, take off the single quotations. That's for strings. If an integer is being passed, it will still work. But when you try passing a variable set as a null string, it tries to input it as a string, and since it's an int type, it fails.

Also, more often than not, you wont need the backticks ` since that's just there to ensure they don't conflict with reserved names.

And I hope this is a pretty simplified example, since you don't show any checking on your variables and are open to sql injection.



Thanks, Styx! Excellent piece of code. You are the master. Hope you can point me and others to where we can learn more about that. It's not the usual if-else statements I come across while googling.
User is offlineProfile CardPM

Go to the top of the page

Styx
post 26 Jun, 2007 - 03:33 AM
Post #4


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


It's called a ternary operator, very useful anime1.gif
User is offlineProfile CardPM

Go to the top of the page

Test
post 12 Aug, 2007 - 11:42 PM
Post #5


New D.I.C Head

*
Joined: 14 Sep, 2006
Posts: 11


My Contributions


Hi all!


Bye
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:29AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month