我对 PHP 很陌生。下面的代码是从我在网上找到的众多教程中拼凑而成的,它正在按照我的意愿工作。我收到了一封来自导师的电子邮件,要求我们添加代码以防止输入重复的电子邮件地址。我有需要添加的代码,但我不知道它应该去哪里。
这是现有的代码:
<?
include('config.php');
// table name
$tbl_name=temp_members;
// Random confirmation code
$confirm_code=md5(uniqid(rand()));
// values sent from form
$email=$_POST['email'];
$password=$_POST['password'];
$firstname=$_POST['firstName'];
$lastname=$_POST['lastName'];
// Insert data into database
$sql="INSERT INTO $tbl_name(confirm_code, email, password, firstname, lastname)VALUES('$confirm_code', '$email', '$password', '$firstname', '$lastname')";
$result=mysql_query($sql);
// if suceesfully inserted data into database, send confirmation link to email
if($result){
// ---------------- SEND MAIL FORM ----------------
// send e-mail to ...
$to=$email;
// Your subject
$subject="Francis Flower confirmation link";
// From
$headers="from: Francis Flower Admin <francis.flower.contact@gmail.com>";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Your message
$message = '<html><head>';
$message .= '<style type="text/css">
body {
font-family: Helvetica, Arial;
}
.center {
text-align: left;
}
</style>';
$message .= '<body><div class="center"><img src="http://www.jblanksby.yourwebsolution.net/images/logo.png"/>';
$message .= "<p>Dear " .$_POST['firstName']. " " .$_POST['lastName'].", </p>";
$message .= '<p>Thank you for signing up for an account at Francis Flower. </p>';
$message .= '<p>Your new account details are below: </p>';
$message .= "<p>Email Address: ".$_POST['email']. "</p>";
$message .= "<p>Password: " .$_POST['password']. "</p>";
$message .= "<p>Before you can login, you need to activate your account using the link below:</p>";
$message .= "<p>Click on this link to activate your account</p>";
$message .= "<p>http://jblanksby.yourwebsolution.net/confirmation.php?passkey=$confirm_code</p>";
$message .= '</div></body></html>';
// send email
$sentmail = mail($to,$subject,$message,$headers);
}
// if not found
else {
echo "Not found your email in our database";
}
// if your email succesfully sent
if($sentmail){ ?>
echo "Mail has been sent";
} else {
echo "Mail has not been sent"};
?>
这是捕获我希望包含在上述代码中的重复电子邮件的代码:
$query = "SELECT * FROM $tbl_name WHERE email = '{$email}'";
$result = mysql_query($query);
if ( mysql_num_rows ( $result ) > 1 )
{
/* Username already exists */
echo 'Username already exists';
}
else
{
/* Username doesn't exist */
/* .. insert query */
}
任何有这个的助手都会很棒!