Reputation: 51
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>';
}
?>
Upvotes: 1
Views: 44
Reputation: 51
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>';
}
?>
Upvotes: 1
Reputation: 7791
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='')");
Upvotes: 0