【问题标题】:Unique constraint throwing a QLSTATE[23000]: Integrity constraint violation: 1062 Duplicate because of unique constraint [duplicate]引发 SQLSTATE [23000] 的唯一约束:违反完整性约束:1062 由于唯一约束而重复 [重复]
【发布时间】:2016-09-20 17:26:09
【问题描述】:

在我的 mysql 数据库中,我将“电子邮件”字段设置为唯一约束。我不希望两个或更多用户拥有相同的电子邮件地址。我创建了这个函数来检查。我只希望在其他用户尝试使用相同地址时运行该功能。这是函数:

    <?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 'email'

(此错误来自脚本)。

我在脚本中间调用了函数。我的问题是我是否应该使用该功能,并且仅在用户尝试使用相同地址时才运行它。先感谢您。

【问题讨论】:

标签: php html mysql function constraints


【解决方案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() 方法不需要遍历每条记录来知道有多少。

编辑:添加完整代码以提高清晰度。

【讨论】:

  • 您的代码改进工作。我仍然收到 1062 错误。不过,我忘了提。 email 是我数据库中的一个索引,它被设置为唯一的。这就是发生 1062 错误的原因。如何避免 1062 错误并让它回显正在使用的电子邮件?
  • 检查我的编辑,我合并了完整的代码,而不仅仅是 sn-ps。也许这会有所帮助。
  • 感谢您的帮助。让它为我工作。重写确实有帮助。再次感谢。 :)
猜你喜欢
  • 2016-01-16
  • 2014-09-16
  • 2016-05-23
  • 2014-08-18
  • 2013-04-22
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多