Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Why won't it check if the e-mail address exists? Properly?
Old 01-15-2008, 03:01 PM Why won't it check if the e-mail address exists? Properly?
Junior Talker

Posts: 2
Name: Vin
Trades: 0
Heyo people. Im new here, thats true, but I do require some help. Im in the process of writing my own PHP Newsletter script, and its about 90% done.

All thats left is PROPERLY writing the subscription.php file.

BEFORE you grill me, I posted this here because Im a newbie. Mods, feel free to move this thread to wherever applicable.

ANYWAYS.

Allowing people to register with my script is no problem. However, Ive noticed if people enter an email address already in the database, it will still add yet another row with that email address.

THIS is NO good.

I need the list to be restricted to one e-mail address per account.

Ive tried multiple things to run the check on existing e-mail addresses, such as mysql_fetch_object();, mysql_fetch_array();, etc. And of course, theyve yet to work.

I was wondering if anyone can help me with this. Id really appreciate it. I figure its got something to do with the if statement not properly verifying the contents of the table row. But, Im no professional, so I probably have no idea what Im talking about.

Here's the code:

Code:
<?php

include = "connect.php";

$connect = mysql_connect($dbhost, $dbuser, $dbpass);

mysql_select_db($dbname) or die ("Unable to connect to database!");

$email = $_POST['email'];
$date = $_POST['date'];

if (isset($_POST['submit'])) {
$query = ("SELECT email FROM subscribers WHERE email = '".$email."'");
$result = mysql_query($query);
    if(mysql_fetch_object($result) == $email){
        print "User already exists!";
    } else {
    $querysub = "INSERT INTO subscribers (email, date) VALUES ('$_POST[email]', '$_POST[date]')";
    $resultsub = mysql_query($querysub);

        echo "Subscription successful. Thank you!";
    }
    mysql_close($connect);
}


else {

print "<form action='".$_SERVER['PHP_SELF']."' method='post'>\n";
print "E-mail Address: <input type='text' name='email'>\n";
print "<input type='hidden' name='date' value='".date("M j, Y")."'>\n";
print "<input type='submit' name='submit' value='Submit'>\n";
print "</form>";

}
?>
If you know a simple fix, please let me know. And if this thread is against any rules, I must've misinterpreted them, and I apologize in advance.

Last edited by PHP Ra; 01-15-2008 at 03:03 PM..
PHP Ra is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-15-2008, 03:19 PM Re: Why won't it check if the e-mail address exists? Properly?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
it's because you don't use the mysql_fetch_object correctly.
PHP Code:
$result mysql_query($query);
$obj=mysql_fetch_object($result);
 if(
$obj->email == $email){
  print 
"User already exists!";
} else {
  
$querysub "INSERT INTO subscribers (email, date) VALUES ('$_POST[email]', '$_POST[date]')";
  
$resultsub mysql_query($querysub);

  echo 
"Subscription successful. Thank you!";

This should correct your behavior.
But you could add a unique index on your table, as a security.
It would prevent from the db the possibility to have several times the same value in a specific row.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 01-15-2008 at 03:20 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-15-2008, 03:43 PM Re: Why won't it check if the e-mail address exists? Properly?
Junior Talker

Posts: 2
Name: Vin
Trades: 0
Thank you. That seemed to have work. I appreciate it +1

Im a little rusty with mysql given Ive usually used files to store information. MySQL has been something I was always interested in using, but never rly got into storing it since files were doing what they needed for the time being. Now that my project will increase in traffic, Im sure a database would be more applicable. Again, thanks .

Also, does this mean whenever I call for an mysql_fetch_row (or whatever function), that it should be noted as a variable?
PHP Ra is offline
Reply With Quote
View Public Profile
 
Old 01-15-2008, 04:36 PM Re: Why won't it check if the e-mail address exists? Properly?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Also, does this mean whenever I call for an mysql_fetch_row (or whatever function), that it should be noted as a variable?
I'm not a native English speaker, and I don't fully grasp the meaning of this sentence...

Whatever you're using, mysql_fetch_object or mysql_fetch_array is irrelevant.
If you look at the documentation of thoses functions, you will see that they return PHP object/array.
So, you need to wrap your code around what the functions gives you back.
I could have written it
PHP Code:
...
$result mysql_query($query);
if(
mysql_fetch_object($result)->email == $email){
... 
But I don't like this much, as if you need another value, you need a mysql_fetch_object again.
This consume resources, always.
So, defining 1 mysql_fetch_xxx() to a variable, and working against that variable is preferable.

Hoping this answers you question.
</span></span>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-16-2008, 01:31 AM Re: Why won't it check if the e-mail address exists? Properly?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Remember that mysql_fetch_row() returns an array with numeric indexes ($row[0], $row[1], ..., $row[n]), mysql_fetch_assoc() returns array with indexes named after fields in table ($row['id'], $row['name'], $row['etc']) and mysql_fetch_object() returns an object-like data structure where fields are also named after table fields ($row->id, $row->name, $row->etc). Thus you should refer to the result of mysql_fetch_*() according to what it returns.

So I prefer to use mysql_fetch_object() everywhere.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 01-16-2008, 06:12 AM Re: Why won't it check if the e-mail address exists? Properly?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
bit unrelated but i have learnt something here ;D
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to Why won't it check if the e-mail address exists? Properly?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.51658 seconds with 12 queries