0

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

1
  • so, where are we with the question/answer? I tested all this; cannot reproduce your problem. Commented Feb 5, 2016 at 0:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

while($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> "; } } Now the row IS empty... made sure of that and it will NOT show my form lol
To tag onto "Make sure they are indeed empty" make sure that empty is the correct function you should be using, because different versions of PHP treat empty differently. php.net/manual/en/function.empty.php
@Luke3butler Good point Luke, thanks. Edit: Added in an edit.
Alright, Ill check and update this soon. Gotta run to the store real quick. Sorry ;)
@Kmiles1990123 no problemo. Keep me posted. I'm always glad to help and provide a solution.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.