I'm using the following script in order to insert: name, email, mobile number. How can I check to see if the email already exisit and if so redirect to another page?
<script langauage="Javascript" type="text/javascript">
var frmvalidator = new Validator("subscribe");
frmvalidator.addValidation("name","req","Please enter your Name");
frmvalidator.addValidation("email","req","Please enter your email address");
frmvalidator.addValidation("mobile","req","Please enter your mobile number");
frmvalidator.addValidation("email","email");
</script>
this is very easy ..
you can either make the field email as a 'unique' index in your table , so mysql will generate an error if the same e-mail was inserted , or use a small SELECT query to check after the form has been submited ..
like this :
PHP Code:
//this one after the ELSE (meaning after form submittion)
} else {
$name=trim(addslashes($_POST['name']));
$email=trim(addslashes($_POST['email']));
$mobile=trim(addslashes($_POST['mobile']));
$link = mysql_connect('localhost','user', 'password') or die("Could not connect");
mysql_select_db('select_db') or die("Could not select database");
$check=mysql_query("SELECT email FROM dada_subscribers WHERE emial='$email'")or die(mysql_error());
$num=mysql_num_rows($check);
if ( $num == 1 ) {
//this should redirect , because we found the same e-mail in DB ..
header("Location: page.php");
} else {
//otherwise we insert the new record
$insert=mysql_query("INSERT INTO dada_subscribers (name, email, mobile, list, list_type, list_status) VALUES ('$name', '$email', '$mobile', '$list', '$list_type', '$list_status')")or
die('couldn\'t Insert record into Subscribers database :'.mysql_error());
echo'The Record was inserted succesfully into Database';
}
}
__________________ PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Warning: Cannot add header information - headers already sent by (output started at /home/httpd/vhosts/selectguestlist.com/httpdocs/index/subscribe.php:8) in /home/httpd/vhosts/selectguestlist.com/httpdocs/index/subscribe.php on line 86
PHP Code:
$link = mysql_connect('localhost','select_user', 'password') or die("Could not connect");
mysql_select_db('select_db') or die("Could not select database");
$check=mysql_query("SELECT * FROM `dada_subscribers` WHERE 1 AND `email` LIKE '$email' LIMIT 0 , 30")or die(mysql_error());
$num=mysql_num_rows($check);
if ( $num == 1 ) {
//this should redirect , because we found the same e-mail in DB ..
header("Location: http://www.selectguestlist.com/index/subscribeFailed.htm");
} else {
//otherwise we insert the new record
$insert=mysql_query("INSERT INTO dada_subscribers (name, email, mobile, list, list_type, list_status) VALUES ('$name', '$email', '$mobile', '$list', '$list_type', '$list_status')")or
die('couldn\'t Insert record into Subscribers database :'.mysql_error());
did you output anything?
this message always pops when you output anything before you sent the header() !
even if it was out of the php starting tag '<?php' .. like plain html ..
__________________ PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
sorry to double post but I couldn't edit my last post!
anyway, what I wanted to say is you can bypass this problem by printing a notice to the user that this email is already in database , and print a link to the page you wanted :
PHP Code:
//replace this IF with the old redirect one ..
if ( $num == 1 ) {
//this should print a notice , because we found the same e-mail in DB ..
echo'The email address you provided is already in use!<BR />';
echo'please try again with another email address (<a href="page.php">Click Here</a>)';
}
__________________ PHPSimplicity
We don't need a reason to help people - Zidane [FF9]