Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy How to check if a field is empty?

    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!


    Code:
    $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 '';
    }

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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)

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    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*.

    Code:
    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.

  • #5
    New Coder
    Join Date
    Jan 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you, that makes more sense!

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

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    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:
    Code:
    $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.

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Old Pedant View Post
    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:
    Code:
    $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,
    PHP Code:
    if(is_empty($year)){ .. 
    best regards

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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().

  • Users who have thanked Fumigator for this post:

    oesxyl (04-23-2009)

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    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
    Code:
    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.

  • #10
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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

    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...e-juggling.php


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •