0
您好Stackoverflow社区,在PHP和PDO中创建一个例外以防止重复
我开始与PDO很快合作。我有一个微不足道的问题,我不知道如何解决。所以,让我知道你们是否可以帮助我。
我有一个表单,旨在更新成员空间中用户帐户的数据。该表格有三个字段“姓氏”,“姓名”和“电子邮件”。
我不希望用户注册和现有的电子邮件。但是,如果用户不想更新其电子邮件并且只想更改“姓氏”和“名称”字段,则PHP代码必须允许更新数据库中的记录。
我创建了一个函数来处理表单。它能够防止重复记录的插入,但它有一个问题。如果用户不想更新他们的电子邮件,该函数返回数据库中已经有相同的电子邮件。事实上,电子邮件已经存在,所以我想知道如何在我的代码中实现此异常,以便在用户不想更改电子邮件时更新记录?
下面的功能:
function update_admin_profile() {
session_start();
if (isset($_SESSION['id']) AND isset($_SESSION['login'])) {
// get the session variables for another propouse.
$id = $_SESSION['id'];
$pseudo = $_SESSION['login'];
// p - It's the URL parameter. anti_sql_injection is a function to check the parameter.
$p = anti_sql_injection($_GET['p']);
if (isset($_POST['last_name']) AND isset($_POST['name']) AND isset($_POST['email'])) {
$bdd = connexion_bdd();
$query = $bdd->prepare('SELECT * FROM tbl__administrators WHERE email = :email');
$query->execute(array('email' => htmlspecialchars($_POST['email'])));
$count=$query->rowCount();
if ($count == 0) {
$update = $bdd->prepare('UPDATE tbl__administrators SET last_name = :last_name, name = :name, email = :email WHERE id = ' . $p);
$update->execute(array(
'last_name' => htmlspecialchars($_POST['last_name']),
'name' => htmlspecialchars($_POST['name']),
'email' => htmlspecialchars($_POST['email'])
));
//The profile was updated.
header('Location: notify.php?m=49');
} else {
//The e-mail already exists!
header('Location: notify.php?m=48');
}
} else {
//Please fill in all fields
header('Location: notify.php?m=41');
}
} else {
//You session is expired. You will be disconnected now. Please, perform the login again and repeat this operation.
header('Location: notify.php?m=7');
}
}
注:这是如果我改变了电子邮件功能的工作原理。
非常感谢您的帮助。
祝您有美好的一天。
非常感谢您的帮助。这很简单。 – lfgoulart