检查重复的电子邮件地址但排除当前用户

时间:2016-12-08 15:51:52

标签: php sql validation pdo duplicates

我创建了一个用户登录系统,并有一个用户可以更新其详细信息的页面。电子邮件地址和用户名必须是唯一的,因此我在更新表单中添加了验证。但是,在提交表单时,错误表明电子邮件地址已被使用。 我可以以某种方式从重复检查中排除当前的电子邮件(存储在会话中)吗?

这是我的代码,但不是全部,因为它有点长,我还需要对用户名进行相同的验证检查:

    try {
        $stmt = $db->prepare('SELECT email, username, firstname, middlename, lastname FROM members WHERE memberID = :memberID');
        $stmt->execute(array(':memberID' => $_SESSION['memberID']));
        $row = $stmt->fetch();
    } catch(PDOException $e) {
        echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        }

    //process form
    if(isset($_POST['submit'])){

        // email validation
        if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
            $error[] = 'Please enter a valid email address';
        } else {
            $stmt = $db->prepare('SELECT email FROM members WHERE email = :email');
            $stmt->execute(array(':email' => $_POST['email']));
            $emailcheck = $stmt->fetch(PDO::FETCH_ASSOC);

            if(!empty($emailcheck['email'])){
                $error[] = 'Email provided is already in use.';
            }
        }

        // insert to DB
        if(!isset($error)){ 

            try {
            $stmt = $db->prepare('UPDATE members SET username=:username, firstname=:firstname, middlename=:middlename, lastname=:lastname, email=:email, updated=NOW() WHERE memberID = :memberID');
                $stmt->execute(array(
                    ':username' => $_POST['username'],
                    ':firstname' => $_POST['firstname'],
                    ':middlename' => $_POST['middlename'],
                    ':lastname' => $_POST['lastname'],
                    ':email' => $_POST['email'],
                    ':memberID' => $_SESSION['memberID']
                ));

            //redirect to page with updated message
            header('Location: personal-details.php?action=updated');
            exit;

            } catch(PDOException $e) {
                $error[] = $e->getMessage();
            }

        } else {
            $error[] = 'Update error';
        }

    }

然后在我使用的每个文本框中:

    <input type="text" name="username" id="username" class="form-control" value="<?php echo($row['username']) ?>" tabindex="1">

1 个答案:

答案 0 :(得分:4)

您可以像在其他查询中一样使用var MockSet = new Mock<DbSet<T>>(); MockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(MockData.AsQueryable().Provider); MockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(MockData.AsQueryable().Expression); MockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(MockData.AsQueryable().ElementType); MockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(() => MockData.AsQueryable().GetEnumerator()); MockSet.Setup(m => m.Add(It.IsAny<T>())).Callback<T>(MockData.AddPlus); // here change te method 'Add' for the extension method 'AddPlus' ,但不包括它:

public static void AddPlus<T>(this List<T> miLista, T item) 
        {
            int nuevoId;
            int? id;
            try
            {
                id = (int)item.GetPropValue("id");
            }
            catch
            {
                id = null;
            }
            if (id == 0)
            {
                if (miLista.Count() > 0)
                {
                    var listaInts = miLista.Select(i => (int)i.GetPropValue("id"));
                    nuevoId = listaInts.Max(x=>x) + 1;
                }
                else
                    nuevoId = 1;
                item.SetPropValue("id",nuevoId);
            }
            miLista.Add(item);
        }