PHP If Else Not working like i thought it would

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TravisT6983
Forum Newbie
Posts: 19
Joined: Wed Jul 02, 2008 2:15 pm

PHP If Else Not working like i thought it would

Post by TravisT6983 »

Hello All I have the code below that i cant seem to get to work.

At the bottom here you will find my code with a If Else statement. That i just cant get to do what i want. so i am hoping someone can help guide me in the right direction. I am gonna be honest i am VERY new to PHP so please bare with me.

On the first If it checks to make sure that the promocode that was entered is in the database and that part works.

On the second if i want it to look through the database and find the promocode and confirm that there is not an email address associated with that promocode record. Currently it is not doing that i can enter a promo code that has an email or not and it inserts into the DB.

This is where my problem lies am i doing this completely wrong is there a better way to accomplish what i am trying to do here? Or have i just overlooked something small?

Code: Select all

			$promosql = "SELECT email IS NULL or email = '' has_email FROM formdata WHERE promoCode = '$varPromo'";
			$promoraw = $mysqli->query($promosql);
			
			if($promoraw->num_rows != 1) {
				$promo .= "$varPromo is not a valid promocode \n";
			} else {
				$row = $promoraw->fetch_assoc();
				if ($row['has_email']) {
				// Promo code has been used
				$dupe .= "$varPromo has already been used on $varDate \n";
				} else {
				// Insert into table
				$sql = "INSERT INTO formdata (promoCode, name, email, address, city, state, zip, submitDate) VALUES (".
						PrepSQL($varPromo) . ", " .
						PrepSQL($varName) . ", " .
						PrepSQL($varEmail) . ", " .
						PrepSQL($varAddress) . ", " .
						PrepSQL($varCity) . ", " .
						PrepSQL($varState) . ", " .
						PrepSQL($varZip) . ", " .
						PrepSQL($varDate) . ")";
				$mysqli->query($sql);
				
				header("location: index.php?success=1");
				exit();
				}					
			}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: PHP If Else Not working like i thought it would

Post by Burrito »

I only see one IF.

Edit...wait now I see two.

Edit x2...
in your second if block you're not searching anything in the database, you look to just be inserting a row.
Post Reply