Jump to content

Need an if doesnt exist in database statement :(


Recommended Posts

Been trying forever to figure this out, driving me nuts!

 

I need this page to search my database and see if the session username exists, if it doesnt it comes back with a form (review form) and also shows a coupon. Now i'm trying to get the coupon bit to work.

 

Basically I am trying to:

IF exists: Your coupon is: blah blah

IF doesn't exist: Review to receive coupon

 

I can't do a like select * where username='w/e' because, comes back with invalid or i dont know how to do it properly, :(

 

at the moment i am trying to make it parse out all the usernames in the table, if any of them = the session name, it makes $answer = true, if not then $answer = false, then later on in code, if true, Your coupon is blah blah, if false, review!

 

This is my coding, when I run it, it notices that $answer = "false" but it still shows the coupon code as if it equals true as well.

 

Any help? :(

<?
$result = @mysql_query("SELECT * FROM salon_data WHERE salon_name='$name' LIMIT 0,1"); 
if (!$result) { 
echo("<strong>Error performing query: " . mysql_error() . "</strong>"); 
exit(); 
}
$sqlx = @mysql_query("SELECT * FROM salon_stats_data WHERE salon_name='$name'"); 
// SQL ERROR CHECK
if (!$sqlx) { 
echo("<b>Error performing query: " . mysql_error() . "</b>"); 
exit(); 
} 

$answer = "";

if($session->logged_in) {
// ----------		
while ($row = mysql_fetch_array($result)) { 
	$salon_coupon = $row["salon_coupon"];
	$salon_name = $row["salon_name"];

	while ($rw2 = mysql_fetch_array($sqlx) ) { 
		$salon_username = $rw2["salon_username"];
		echo "Salon Username: $salon_username<br>";

		if ($salon_username == $session->username) {
		$answer = "true";
		} else {
		$answer = "false";
		}
	}
}
// ----------	
}
echo "<br> ($answer) <br>";
// Check Answer
if ($answer = "true") {
echo "<strong>$session->username</strong>. <br /><img src='img/action_check.png'> Your Coupon for <strong>$salon_name</strong> is: <span id='stat'><strong>$salon_coupon</strong></span><br /><br />";
}
if ($answer = "false") {
echo "Hello $session->username! If you rate and review this salon, you'll be eligible for a coupon!";
}
// If user not logged in:
if(!$session->logged_in){
echo "Hello! <a href='?c=a1'>Register now</a> to rate and review this salon, you'll be eligible for a coupon!<br /><br />"; 
}
?>

 

if ($answer = "true") {

 

should be

 

if ($answer == "true") {

 

same for 'false' of course

 

What you did wrong:

= is for assigning values.

== is for comparing values

 

When you use = in if(), PHP assigns a value ("false") to variable ($answer) and says "hey I did it!" - which means the if() evaluates as true.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.