Jump to content

Recommended Posts

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?

$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();
				}					
			}
if ($row['has_email']) {

This will be true when there is NO email (ie if it is null or blank) so your test needs to be

if (!$row['has_email']) {

to see if there is an email address

 

edit : perhaps a better alias would be "empty_email"

Edited by Barand

Thanks You! @Barand that is now making it work as i expected it too... 

 

Your probably right about the alias. 

 

Like i mentioned i am VERY new this PHP stuff. Can you kind of explain the difference between what i wrote and how you fixed it?

I thought I had but I'll try again. This time I'll use the revised alias name as that it probably what was causing the confusion. Having a field called "has_email" that is true when there is NO email is illogical to me.

SELECT email IS NULL or email = '' empty_email FROM formdata WHERE promoCode = '$varPromo'

"empty_email" is true if there is no email.

 

Just by changing the alias from "has_email" to "empty_email" the PHP if statement becomes more readable and makes logical sense

if (!$row['empty_email']) {     // if the email is not empty

I gotcha that does make more logical sense to me as well. That was actually code that was suggested to me from another forum. But really all they were doing were telling me i am crappy coder which in this case i know i am not great. But they weren't offer to help just kinda bashing me. I appreciate you taking the time to go though it and explain it. Cheers have a great weekend @Barand

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.