Travist6983 Posted October 18, 2013 Share Posted October 18, 2013 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(); } } Quote Link to comment Share on other sites More sharing options...
davidannis Posted October 18, 2013 Share Posted October 18, 2013 (edited) try if (trim($row['has_email'])!='') to see if it is blank instead of just if ($row['has_email']) Edited October 18, 2013 by davidannis Quote Link to comment Share on other sites More sharing options...
Travist6983 Posted October 18, 2013 Author Share Posted October 18, 2013 Thanks for the reply @davidannis It is now throwing my error message that the promocode has already been used but it hasnt been. I am using a blank record from the database. Any other suggestions? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 18, 2013 Share Posted October 18, 2013 (edited) 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 October 18, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
Travist6983 Posted October 18, 2013 Author Share Posted October 18, 2013 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 18, 2013 Share Posted October 18, 2013 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 Quote Link to comment Share on other sites More sharing options...
Travist6983 Posted October 18, 2013 Author Share Posted October 18, 2013 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.