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.