【问题标题】:Check for duplicate emails before querying查询前检查重复的电子邮件
【发布时间】:2013-09-06 01:40:29
【问题描述】:

我已经搜索并查看了其他示例,但由于我是新手,因此无法转换为我的代码。我想做的是让它检查数据库以查看是否已经输入了该电子邮件。如果有,我希望它告诉他们有,并且每人只有一个条目。任何帮助将不胜感激。请记住,这是我编写的第一个连接到数据库的代码。

<?php //data.php


// Get values from form
$name        = $_POST['name'];
$email        = $_POST['email'];

$user_name = "user";
$password = "password";
$database = "dbname";
$server = "ahostsomewhere";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {
$SQL = "INSERT INTO contestant_drawing (name, email) VALUES ('" . $name . "', '" .    $email . "')";
$result = mysql_query($SQL);
echo "To finalize your entry like our FaceBook Page, Good Luck!";

mysql_close($db_handle);


}else {
print "Database NOT Found ";
mysql_close($db_handle);

}
?>

【问题讨论】:

  • 第一条评论:如果这是您编写的第一个连接到数据库的代码,请正确执行。不要使用mysql - 它已被弃用。重新编写代码以使用mysqli(直截了当)或PDO,(但这涉及更多)
  • 2 个选项,先进行选择或将电子邮件设为唯一索引,始终尝试插入,并检查是否因重复而失败

标签: php mysql insert duplicates


【解决方案1】:

第一件事:切换到Prepared Statements

它们是访问数据库的更安全和更先进的方式。

<?php

// Get values from form
$name        = $_POST['name'];
$email       = $_POST['email'];

$user_name    = "user";
$password     = "password";
$database     = "dbname";
$server       = "ahostsomewhere";

//Connect to your database using PDO (this only needs to be done once). $dbh is our connection
try {
    $dbh = new PDO("mysql:host=$server;dbname=$database", $user_name, $password);
}

//Make sure there are no errors
catch(PDOException $e){
    echo($e->getMessage());
}

//Query to check if the email already exists    
//This prepares the statements and uses placeholders (designated with a ':' colon)
$stmt = $dbh->prepare("SELECT * FROM `contestant_drawing` WHERE `email`=:email")
//This then binds a string to the placeholder (note the string '$stmt' is constant here)
$stmt->bindParam(':email',$email);
//Finally we execute the query
$stmt->execute();

//Count the rows in the returned array to see if there are already matching values in the database
if($stmt->rowCount()!=0){
    //Email already registered. Exit with a message
    exit('Email already exists');
}

//Email OK, continue with your queries
//You can use the same string '$stmt' because we don't need the query from before anymore. If you had multiple queries running alongside one another then you could use different strings for $stmt ($stmt1, $stmt2, $foo, $bar etc) but we can keep it the same to keep things simple
$stmt = $dbh->prepare("INSERT INTO `contestant_drawing`
                        (`name`, `email`)
                        VALUES (:name, :email)");
$stmt->bindParam(':name',$name);
$stmt->bindParam(':email',$email);
$stmt->execute();

echo "To finalize your entry like our FaceBook Page, Good Luck!";

//Disconnect from the database ($dbh)
$dbh = NULL;

?>

我所做的是执行一个单独的查询,首先使用用户的电子邮件地址搜索表中已经存在的任何条目。只要没有找到任何东西,脚本就会继续。

希望这也能让您深入了解如何执行准备好的语句。这些可确保您的数据库不会被使用注入来篡改,这对您来说很不利,并确保您可以专注于编写高效的脚本而不是清理用户输入。

【讨论】:

  • 我在 stackoverflow 上的第一篇文章,我很惊讶。感谢您提供广泛的 cmets。我在哪里可以找到更多关于使用 PDO 的信息,有什么好的在线链接吗?
  • 使用您的示例时,我收到错误警告:PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens在 /home/content/88/10316188/html/contest/data.php 第 43 行
  • 没关系,我通过添加 $stmt->bindParam(':email',$email); 行解决了它
  • 您可以找到更多关于 Prepared Statements 和 PDO herehere,以及我的答案顶部的链接。如果它对您有所帮助,请不要忘记通过单击勾选将其选为您接受的答案:)
猜你喜欢
  • 2011-12-02
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 2015-01-24
  • 2018-09-09
  • 1970-01-01
  • 2019-07-23
  • 2017-04-23
相关资源
最近更新 更多