Kmiles1990123
Kmiles1990123

Reputation: 189

php EMPTY not working correctly

I am trying to display a form ONLY if the current row is empty.. Currently the row in my DB is NOT empty. It has a date in it.. 2/10/16 it is set as a VARCHAR though. Anyways, When I use the following.. It still shows me the form even though it shouldnt because the row is NOT empty..

Below is the code

order = $_GET['uid'];
$stmt=$this->db->prepare("SELECT * FROM orders WHERE order_num = :order");
$stmt->execute(array(":order"=>$order));
$row=$stmt->fetch(PDO::FETCH_ASSOC);

if (empty($row['giventoatpdate'])) {
    echo 
    "
    <form name='atpdate' method='POST'>
    Date Sent to clown?
    <br>
    <input type='text' name='update'>
    <br>
    <input type='submit' name='giventoatpdate'>
    </form>
    ";
}

All variable names are correct and spelled correctly.

The select statement is working aswell, Already tested.. I feel it has to do with empty and the data in teh db being a date. 2/10/16

Upvotes: 0

Views: 177

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74232

You need to use a while loop:

while($row=$stmt->fetch(PDO::FETCH_ASSOC)){

    if (empty($row['giventoatpdate'])){
    ...
    }
    else{
    ...
    }

}

and make sure that those empty rows, if empty... are indeed empty.

Alternatively and as stated by Luke in comments, different PHP versions could empty() differently.

if ($row['giventoatpdate'] == ''){...}

or another alternative:

if ($row['giventoatpdate'] != ''){...} // using a negation instead

and inside that while loop.

  • IMPORTANT: Make sure you do not have a default value for the column. If that default value is 0 or NULL or other, then that could affect your query and will never be considered as being empty.

  • Another to check for, is if your UPDATE didn't introduce any whitespace. If the said column contains a space, then that will also be considered as "not empty". This being the fact upon testing for that also.


Edit:

After testing, your query may be failing because you are selecting all of your columns with SELECT * rather than the giventoatpdate column itself.

This being that there are other columns that are not empty.

Therefore, you may need to select the column itself only in the query:

SELECT giventoatpdate FROM orders

However, make sure you did select the right column.

N.B. I just did another test with SELECT * and it also worked, so you can scratch my above theory, but not totally leave it out of the equation though.

Upvotes: 1

Related Questions