Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP和PDO中创建异常以防止重复_Php_Mysql_Pdo_Duplicates - Fatal编程技术网

在PHP和PDO中创建异常以防止重复

在PHP和PDO中创建异常以防止重复,php,mysql,pdo,duplicates,Php,Mysql,Pdo,Duplicates,大家好,我的社区 我很快就要开始和PDO合作了。我有一个我不知道如何解决的小问题。所以,如果你们能帮我,请告诉我 我有一个表单,旨在更新成员空间中用户帐户的数据。此表单有三个字段“姓氏”、“姓名”和“电子邮件” 我不希望用户注册现有的电子邮件。但是,如果用户不想更新他们的电子邮件,只想更改“lastname”和“Name”字段,那么PHP代码必须允许更新数据库中的记录 我创建了一个函数来处理表单。它能够防止插入重复记录,但存在问题。如果用户不想更新他们的电子邮件,函数将返回数据库中已经存在相同的

大家好,我的社区

我很快就要开始和PDO合作了。我有一个我不知道如何解决的小问题。所以,如果你们能帮我,请告诉我

  • 我有一个表单,旨在更新成员空间中用户帐户的数据。此表单有三个字段“姓氏”、“姓名”和“电子邮件”

  • 我不希望用户注册现有的电子邮件。但是,如果用户不想更新他们的电子邮件,只想更改“lastname”和“Name”字段,那么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');
        }
    }
    
    注意:如果我更改电子邮件,它的功能将起作用


    非常感谢你的帮助

    祝你今天愉快

    如果用户不想更新他们的电子邮件,函数将返回数据库中已经存在相同的电子邮件

    这很简单。只需添加另一个条件即可将当前用户从查询结果中排除

    $query = $bdd->prepare('SELECT 1 FROM tbl__administrators WHERE email = ? and id != ?');
    $query->execute(array($_POST['email'], $id));
    

    非常感谢你的帮助。这真的很简单。