【问题标题】:How can I change the default error message code如何更改默认错误消息代码
【发布时间】:2017-03-09 12:55:59
【问题描述】:

我想将错误代码 SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'xx@yahoo.com' for key 'email' message 更改为“error email already in use”之类的内容

创建.php

<?php
require_once 'dbconfig.php';

if ($_POST) {

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $contactnum = $_POST['contactnum'];
    $email = $_POST['email'];
    $pass = $_POST['pass'];
    $lang = $_POST['lang'];

    try {

        $stmt = $db_con->prepare("INSERT INTO tbluser(fname,lname,contactnum,email,pass,lang) VALUES(:ufname,:ulname,:ucontact,:uemail,:upass,:ulang)");
        $stmt->bindParam(":ufname", $fname);
        $stmt->bindParam(":ulname", $lname);
        $stmt->bindParam(":ucontact", $contactnum);
        $stmt->bindParam(":uemail", $email);
        $stmt->bindParam(":upass", $pass);
        $stmt->bindParam(":ulang", $lang);

        if ($stmt->execute()) {
            echo "Successfully Added";
        } else {
            echo "Query Problem";
        }

    } catch(PDOException $e) {
        echo $e->getMessage();
    }
}
?>

我尝试添加类似这样的内容

if (mysql_errno() == 1062) {

    echo"error email already in use!";
}

但不能让它工作。感谢您的帮助,我还是 php 新手。有人可以告诉我如何使用 PDO 吗?

addform.php

<style type="text/css">
    #dis{
        display:none;
    }
</style>

<div id="dis">
    <!-- here message will be displayed -->
</div>

<form method='post' id='emp-SaveForm' action="#">

    <table class='table table-bordered'>

        <tr>
            <td>First Name</td>
            <td><input type='text' name='fname' class='form-control'  required /></td>
        </tr>

        <tr>
            <td>Last Name</td>
            <td><input type='text' name='lname' class='form-control'  required></td>
        </tr>

        <tr>
            <td>Contact Number</td>
            <td><input type='number' name='contactnum' class='form-control'  required></td>
        </tr>

         <tr>
            <td>Email</td>
            <td><input type='email' name='email' class='form-control' required /></td>
        </tr>

         <tr>
            <td>Password</td>
            <td><input type="password" name='pass' class='form-control'  required /></td>
        </tr>

         <tr>
            <td>Language</td>
            <td><input type='text' name='lang' class='form-control'  required /></td>
        </tr> 
        <tr>
            <td colspan="2">
            <button type="submit" class="btn btn-primary" name="btn-save" id="btn-save">
            <span class="glyphicon glyphicon-plus"></span> Save this User
            </button>  
            </td>
        </tr>

    </table>
</form>

【问题讨论】:

  • 您需要使用 SELECT 来检查电子邮件是否存在,如果电子邮件不存在则仅执行 INSERT
  • 为什么不创建一个存储过程来代替那个。

标签: php mysql sql mysqli pdo


【解决方案1】:

如果您试图查找合法错误,错误可能会出现在事件/错误日志中并导致不必要的噪音,所以如果可以的话,我会避免依赖它们。而是尝试:

IF (SELECT 1 = 1 FROM users WHERE email=?) THEN
BEGIN
    SELECT 0 AS Result--signals already exists to the application
END;
ELSE
BEGIN
    INSERT INTO users(columns) VALUES(values);
    SELECT LAST_INSERT_ID() AS Result;
END;
END IF;

或者类似的东西。

【讨论】:

  • 当两个用户同时使用同一个电子邮件注册时会发生什么?仍然需要处理插入错误,并发意味着您不能信任选择来防止违反约束。
  • @casey 哦,当然要保留表上的约束。问题在于您的流程依赖它,并且您的日志中有很多不必要的错误。如果您收到错误,请将其视为错误,而不是正常流程的一部分
【解决方案2】:

mysql_errno 是已弃用的 mysql 扩展的一部分 - 您正在使用 PDO,因此您也需要检查错误:

if ($stmt->execute()) {
    echo "Successfully Added";
} else {
    if ($stmt->errorCode() == 1062) {
        # Here ^
        echo "error email already in use!";
    } else {
        echo "some other problem...";
    }
}   

【讨论】:

    猜你喜欢
    • 2018-06-04
    • 2018-12-01
    • 1970-01-01
    • 2011-10-17
    • 2016-12-22
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多