1

I have a database that has 2 records in it:

id |  message  |    date     |   user    | option
---+-----------+-------------+-----------+--------
1  |  Welcome  |  2015-03-01 |           |   0
2  |  message  |  2015-03-05 |  admin    |   0

What I'm trying to do is if the user field is blank, it shows the message to all users, if there is a username (such as admin in this example) listed, it shows the one for that user, and a the blank message if it exist.

Right now it will show both messages if the user is Pam (it should only show id 1).

If the user is admin, it shows both messages.

It seems like its ignoring the user = '$zuser'

What am I doing wrong?

<?php 

  ini_set('display_errors',1);  error_reporting(E_ALL);
  $zuser=$_COOKIE['aauser'];

  $result = mysqli_query($con,"SELECT * FROM message WHERE (user = '$zuser' OR run_date >= CURDATE() AND user='')");

  while($quick = mysqli_fetch_array($result))           
  {
    echo $quick['message'];
    echo '<script>alert("'.$quick['message'].'");</script>';
  }

?>
10
  • Run a var_dump on the cookie while making sure it's set/not empty. Also or die(mysqli_error($con)) to mysqli_query() Commented Mar 1, 2015 at 3:20
  • there is also a difference between null and '' (empty string) Commented Mar 1, 2015 at 3:24
  • 1
    Entered... meaning what? Plus, where is the cookie initially set? Remember, cookies only get set AFTER the second page load. Commented Mar 1, 2015 at 3:25
  • 1
    if(isset($_COOKIE['aauser'])){...} that will determine if it's set. Or if(!empty($_COOKIE['aauser'])){...} Commented Mar 1, 2015 at 3:33
  • 1
    @Ghost Problem solved... ;-) Commented Mar 1, 2015 at 3:43

2 Answers 2

1

Found my problem @ Fred -ii- that was a bone head mistake i forgot that cookies were only set after the second page loaded!!!

Thank you for your help!

<?php 
        ini_set('display_errors',1);  error_reporting(E_ALL);
        echo $zuser=$_GET['usrname'];


    $result = mysqli_query($con,"SELECT * FROM message WHERE user = '$zuser' OR run_date >= CURDATE() AND user=''");

                while($quick = mysqli_fetch_array($result)) 

                {
                echo $quick['message'];
                echo '<script>alert("'.$quick['message'].'");</script>';
                }


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

1 Comment

Blowing on fingers ;-) you're welcome. Cheers!
0

Change this line:

$result = mysqli_query($con,"SELECT * FROM message WHERE (user = '$zuser' OR run_date >= CURDATE() AND user='')");

to:

$result = mysqli_query($con,"SELECT * FROM message WHERE user = '$zuser' OR ( run_date >= CURDATE() AND user='')");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.