...

View Full Version : How to check if a field is empty?



natie769
04-22-2009, 03:27 PM
I have a form that submits the data to mysql. Then on the page where it displays all the rows in the table (items) I want it to only show the form fields (or columns) that have been filled out. So I'm trying to check if it is empty. I'm using a code that does that now -- but it only works for the first entry in the database, and then applies that to each and every entry after it (even though it isn't true)**. Should I be using a for loop so it checks every database table entry, and not just the first entry?

** When I say it applies to each and every entry I mean if the first entries "Category" is empty, it thinks the rest of the table entries "Category" is empty, when in fact it isn't.

Hope that makes sense...
Any help is appreciated! :)




$result = mysql_query("SELECT * FROM `Equipment Listings` WHERE id = $id");

while($row = mysql_fetch_assoc($result)){

echo '<table>';

//CATEGORY
if (isset($_POST['category']))
{
echo '<tr><td><span class="equipmentheader">Category:</span> ';
echo $row['category'];
echo '</td></tr>';
}
else
{
echo '';
}


//YEAR
if (isset($_POST['year']))
{
echo '<tr><td><span class="equipmentheader">Year:</span> ';
echo $row['year'];
echo '</td></tr>';
}
else
{
echo '';
}


//SERIAL NUMBER
if (isset($_POST['serialnum']))
{
echo '<tr><td><span class="equipmentheader">Serial Number:</span> ';
echo $row['serialnum'];
echo '</td></tr>';
}
else
{
echo '';
}

Fumigator
04-22-2009, 05:47 PM
Why are you checking $_POST to determine whether an element should be displayed? Don't you want to check the actual element's value? (If that is empty, don't display it)

natie769
04-22-2009, 08:24 PM
I'm not sure how to go about checking for an empty value -- without having the problem that I'm having now.

How should I do that?

Old Pedant
04-22-2009, 08:47 PM
Fumigator's point is that your CODE is checking to see if the <FORM METHOD=POST> value is empty or not. YOU ARE *NOT* CHECKING the values you get from the database, *AT ALL*.



if (isset($_POST['year'])


$_POST['year'] is looking at a form field (e.g., <input name="year"> or maybe <select name="year">).

isset( ) is looking to see if that form field is empty or not.

Nothing whatsoever to do with whether there is any data in the DB in that column.

natie769
04-23-2009, 01:19 AM
Thank you, that makes more sense!

How should I go about this then (What code do I need to use to achieve this)?

Old Pedant
04-23-2009, 01:56 AM
Not a PHP person. I assume you could just assign the value from the $row field to a variable and then check the variable for null/blank.

Example:


$year = $row["year"]
if ( $year == "" ) ...


But if the field is NULL in the DB, you might need to use some function to test for null value, instead.

oesxyl
04-23-2009, 02:03 AM
Not a PHP person. I assume you could just assign the value from the $row field to a variable and then check the variable for null/blank.

Example:


$year = $row["year"]
if ( $year == "" ) ...


But if the field is NULL in the DB, you might need to use some function to test for null value, instead.
for php, is_empty is safer, :)


if(is_empty($year)){ ..


best regards

Fumigator
04-23-2009, 07:16 PM
It's actually just empty(), not is_empty(). (Don't ask me why, since all the other functions are is_* and I've made that same mistake plenty of times!) But either way, the empty() function annoys me because a value of "0" will return true. There are times I have a value of "0" that means something and it certainly isn't empty! So I usually end up checking both empty() and strlen().

Old Pedant
04-23-2009, 10:15 PM
Ummm...isn't checking strlen( ) kind of silly for a field that's a number or datetime value in the database???

Don't get me wrong, not objecting to your code, but SURELY there should be *SOMETHING* in PHP that would make a proper "is_null( )" check?

If not, I guess the only answer is to do the check in your MySQL code, no?

Something like

SELECT IFNULL(thedate,'1899-01-01') AS theNonNullDate, IFNULL(thenumber,-999999999) AS theNonNullNumber ...

And then check for those oddball values in the PHP code and *treat* them as NULL??

Ugly. Seems like a horrible deficiency in PHP.

Fumigator
04-23-2009, 11:16 PM
PHP has very weak type casting, so even a column with type=integer when assigned to an array element will be "auto-cast" (my own term for it) to a string if you attempt to use a string function on it (such as strlen). One of the charming qualities of PHP :D

There is a PHP function is_null(), but a null value in PHP is not the same thing as a null value in a RDBMS database.

Since I know you're dying to learn PHP, here's a link to fun info on types:

http://us.php.net/manual/en/language.types.intro.php

And more specifically:

http://us.php.net/manual/en/language.types.type-juggling.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum