检查电子邮件是唯一的php [英] Check email to be unique php
问题描述
我正在创建一个关于音乐的网站,在我的网站底部,我有一个只提交电子邮件的表单。我正在这样做,以收集用户的电子邮件。另外,我创建了一个数据库,在这个数据库中,我创建了一个名为emails的表,只有两列: id
和电子邮件
。
我想让我收到的电子邮件是唯一的,因为现在我正在测试它,我可以放置许多同名的电子邮件。我已经尝试了一些编码,以实现这一点,但没有运气,所以我将只发布我现在的代码,所以你可以帮助我与我需要添加的代码,以获得一个唯一的电子邮件地址。 / p>
PS我将在项目结束时将我的数据库从mysql更新为mysqli。
这是我目前的代码:
<?php
error_reporting(E_ERROR | E_PARSE);
include('../ database / db.php');
$ email = $ _POST ['email'];
if($ email!=){
$ sql = mysql_query(INSERT INTO emails(email)VALUES('$ email'));
echo谢谢你的提交,重定向到主页;
}
?>
error_reporting E_ERROR | E_PARSE);
include('../ database / db.php');
$ email = $ _POST ['email'];
if($ email!=){
$ result = mysql_query(SELECT * FROM emails where email ='。$ email。');
$ num_rows = mysql_num_rows($ result);
if($ num_rows> = 1){
echoemail exists;
} else {
$ sql = mysql_query(INSERT INTO emails(email)VALUES('$ email'));
echo谢谢你的提交,重定向到主页;
}
}
用以下代码替换您的代码:)
I am creating a website about music and at the bottom of my website I have a form that has only email submission. I am doing this in order to gather email from the users.
Moreover, I have created a database and in that database I have created a table called emails with only 2 columns:id
and email
.
I would like to make the emails that I receive unique, because now that I am testing it I can put many emails with the same name. I have tried some coding in order to achieve this but no luck so I will post only the code I have at the moment, so you can help me with what I need to add in that code in order to receive a unique email address.
P.S I will update my database from mysql to mysqli at the end of the project.
Here is my current code:
<?php
error_reporting(E_ERROR | E_PARSE);
include('../database/db.php');
$email = $_POST['email'];
if($email != "") {
$sql = mysql_query ("INSERT INTO emails (email) VALUES ('$email')");
echo "Thank you for Submitting. Redirecting back to Home Page";
}
?>
error_reporting(E_ERROR | E_PARSE);
include('../database/db.php');
$email = $_POST['email'];
if($email != "") {
$result = mysql_query("SELECT * FROM emails where email='".$email."'");
$num_rows = mysql_num_rows($result);
if($num_rows >= 1){
echo "email exist";
}else{
$sql = mysql_query ("INSERT INTO emails (email) VALUES ('$email')");
echo "Thank you for Submitting. Redirecting back to Home Page";
}
}
replace your code with this :)
这篇关于检查电子邮件是唯一的php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!