在我的mysql数据库中,我将'email'字段设置为唯一约束。我不希望两个或更多用户拥有相同的电子邮件地址。我创建了这个函数来检查它。我只希望在其他用户尝试使用相同的地址时运行该函数。这是功能:
<?php
function Email_gogo() {
if(!empty($_POST['email']))
{
$mysql_hostname = '*****';
$mysql_username = '*****';
$mysql_password = '*****';
$mysql_dbname = '*****';
try {
$db= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", `enter code here`$mysql_username, $mysql_password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
exit( $e->getMessage() );
}
$query_email = "
SELECT
email
from users
where
email = :email
";
$query_goes = array(
':email' => $_POST['email']
);
Try{
$stmt = $db->prepare($query_email);
$stmt ->execute($query_goes);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
}
}
catch(PDOException $ex){
echo 'ERROR: '. $ex->getMessage();
}
if($stmt->rowCount() > 0){
echo("That Email is already in use...");
}
}
}
?>
在脚本中调用此函数,允许管理员检查用户名,电子邮件和姓氏。用户名无法更改。这是我更新的脚本。这是脚本:
<?php
require("common.php");
require_once("gogo.php");
if(empty($_SESSION['user']))
{
header("Location: ../hound/login.php");
die("Redirecting to ../hound/login.php");
}
if(!empty($_POST))
{
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
die("Please enter a valid email address...");
}
}
Email_gogo();
$array_value = array(
':email' => $_POST['email'],
':first_name' => $_POST['first_name'],
':last_name' => $_POST['last_name'],
':id' => $_POST['id']
);
$query = "UPDATE users
SET
email = :email,
first_name = :first_name,
last_name = :last_name
WHERE
id = :id
";
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($array_value);
}
catch(PDOException $ex)
{
die("Ouch, failed to run query: " . $ex->getMessage());
}
header("Location: users.php");
die("Redirecting to users.php");
?>
这是错误:该电子邮件已在使用中(来自该功能)。它确实检查电子邮件是否正在使用,但它会引发另一个错误:
哎呀,无法运行查询:SQLSTATE [23000]:违反完整性约束:1062 Duplicata du champ'marx@fun.org'pour la clef'mail'
(此错误来自脚本)。
我在脚本中间调用了该函数。我的问题是我应该使用该功能,并且仅在用户尝试使用相同地址时才运行。先感谢您。
答案 0 :(得分:1)
问题是你的函数实际上没有返回任何东西,它只显示一条错误消息,之后PHP将继续正常执行。因此,无论电子邮件是否正在使用,您的“更新”查询都将被执行。这是你应该在Email_gogo
中做的function Email_gogo()
{
if(!empty($_POST['email']))
{
$mysql_hostname = '*****';
$mysql_username = '*****';
$mysql_password = '*****';
$mysql_dbname = '*****';
try
{
$db= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", `enter code here`$mysql_username, $mysql_password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
exit( $e->getMessage() );
}
$query_email = "
SELECT
email
from users
where
email = :email
";
$query_goes = array(
':email' => $_POST['email']
);
try
{
$stmt = $db->prepare($query_email);
$stmt ->execute($query_goes);
}
catch(PDOException $ex)
{
echo 'ERROR: '. $ex->getMessage();
}
if($stmt->rowCount() > 0)
return false;
else
return true;
}
}
然后当你稍后再打电话时:
if(Email_gogo())
{
$array_value = array(
':email' => $_POST['email'],
':first_name' => $_POST['first_name'],
':last_name' => $_POST['last_name'],
':id' => $_POST['id']
);
$query = "UPDATE users
SET
email = :email,
first_name = :first_name,
last_name = :last_name
WHERE
id = :id
";
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($array_value);
}
catch(PDOException $ex)
{
die("Ouch, failed to run query: " . $ex->getMessage());
}
header("Location: users.php");
die("Redirecting to users.php");
}
else
die("Email address already in use");
此外,函数中的“while”循环完全没必要。你可以删除它。 rowCount()
方法不需要遍历每条记录就知道有多少记录。
编辑:添加完整代码以更清晰。