php mysql 重复提交数据_php – 防止将重复数据输入mysql数据库

我正在尝试使我的电子邮件订阅服务拒绝我的数据库中已存在的电子邮件,因此用户不会订阅相同的电子邮件两次.这是我的,但它不起作用,任何想法?

if(!isset($_POST['submit']))

exit();

$vars = array('email');

$verified = TRUE;

foreach($vars as $v) {

if(!isset($_POST[$v]) || empty($_POST[$v])) {

$verified = FALSE;

}

}

if(!$verified) {

echo "

*Email required*

";

exit();

}

$email = $_POST['email'];

if($_POST['submit']) echo "

*Check your inbox*

";

// Create connection

$con=mysqli_connect("mysql.host","user","password","dbname");

// Check connection

if (mysqli_connect_errno($con))

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

$sql="INSERT INTO emails (email) VALUES ('$_POST[email]')";

if (!mysqli_query($con,$sql))

{

die('Error: ' . mysqli_error($con));

}

$query = mysql_query("SELECT * FROM emails WHERE email='$email'",($con));

if(mysql_num_rows($query) != 1)

{

echo "email already exists";

// redirect back to form and populate with

// data that has already been entered by the user

}

mysqli_close($con);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值