【发布时间】:2013-12-26 23:38:40
【问题描述】:
我开发了一个将数据发送到 sql 数据库的电子邮件表单。但是,我注意到可能存在重复的电子邮件。有什么我可以添加到我的 php 或 sql 来防止重复电子邮件的吗?
<?php
if (isset($_POST['submit']) && strlen($_POST['email'])>0 && strlen($_POST['city'])>0)
{
$good_input = true;
$email=$_POST['email'];
$city=$_POST['city'];
mysql_connect("host", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
mysql_query("INSERT INTO `launchpage` VALUES ('$email', '$city')");
}
?>
【问题讨论】:
-
在
email上添加unique index。 -
你应该考虑使用 mysqli 或 PDO,因为 mysql 已被弃用。