Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-15-2007, 07:19 AM   PM User | #1
mtd
Regular Coder

 
Join Date: Jun 2003
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
mtd is an unknown quantity at this point
if/else not working with mySQL command?

I'm trying to set up a basic email list, starting with the following:
PHP Code:
if($_POST['gf_email_signup']=='Yes') {
    
$sql "SELECT email from e_mailing_list where e_mailing_list.email = '$_POST[gf_email_signup]'";
    
$result mysql_query($sql$conn) or die(mysql_error());
    if(
mysql_num_rows($result) != 0) {
        
$email_signup_notice '(Your email address was already on our mailing list)';
    } else {
        
$sql "INSERT INTO e_mailing_list values ('','$date','$_POST[gf_name_first]','$_POST[gf_name_last]','$_POST[gf_email]')";
        
$result mysql_query($sql$conn) or die(mysql_error());
        
$email_signup_notice '(Your email address was successfully added to our mailing list)';
    }

The problem is, after an email address is added, if that address is attempted to be added again (i.e. test@email.com is already in the list but someone tries to add it again), the if/else clause does not work. It skips to the else section and then returns as mySQL error because the email field is set as unique. Am I missing something as to why the if/else is not working to detect a duplicate address? Am I going about this the wrong way?

My apologies if this belongs in the mySQL forum; I am new to mySQL but this seemed to me to be a php problem... but again, I'm new to mySQL so I could be wrong, and maybe the solution is quite obvious to a more seasoned coder...

Thanks in advance.
mtd is offline   Reply With Quote
Old 07-15-2007, 09:26 AM   PM User | #2
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
try echoing out the result of mysql_num_rows - its the only condition in your if so thats the first place to start
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 07-15-2007, 02:10 PM   PM User | #3
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
I would suggest you echo $sql to make sure it contains what you expect and/or proof read your code (you will find that your query is referencing the $_POST variable - gf_email_signup, which apparently contains "Yes" at the time of the query, when it should be using the $_POST variable - gf_email.)
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 07-15-2007, 04:11 PM   PM User | #4
mtd
Regular Coder

 
Join Date: Jun 2003
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
mtd is an unknown quantity at this point
Thanks! I echoed mysql_num_rows and it was as it should be. Then, of course, I saw that in the query I had mixed up gf_email_signup with gf_email - good catch (obviously nobody's email address was going to equal 'Yes', as the query was erroneously set up to check)!!! And of course, I feel embarassed that it was something so simple, but thanks to both of you for your help. Sometimes it just takes an outside eye.

Cheers!
Matt
mtd is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:34 AM.


Advertisement
Log in to turn off these ads.