• <bdo id='xx9cc'></bdo><ul id='xx9cc'></ul>

      <small id='xx9cc'></small><noframes id='xx9cc'>

      <tfoot id='xx9cc'></tfoot>
      <i id='xx9cc'><tr id='xx9cc'><dt id='xx9cc'><q id='xx9cc'><span id='xx9cc'><b id='xx9cc'><form id='xx9cc'><ins id='xx9cc'></ins><ul id='xx9cc'></ul><sub id='xx9cc'></sub></form><legend id='xx9cc'></legend><bdo id='xx9cc'><pre id='xx9cc'><center id='xx9cc'></center></pre></bdo></b><th id='xx9cc'></th></span></q></dt></tr></i><div id='xx9cc'><tfoot id='xx9cc'></tfoot><dl id='xx9cc'><fieldset id='xx9cc'></fieldset></dl></div>

        <legend id='xx9cc'><style id='xx9cc'><dir id='xx9cc'><q id='xx9cc'></q></dir></style></legend>

        如何处理重复条目的错误?

        时间:2023-09-21

      1. <legend id='p95Vw'><style id='p95Vw'><dir id='p95Vw'><q id='p95Vw'></q></dir></style></legend>

        <small id='p95Vw'></small><noframes id='p95Vw'>

        <i id='p95Vw'><tr id='p95Vw'><dt id='p95Vw'><q id='p95Vw'><span id='p95Vw'><b id='p95Vw'><form id='p95Vw'><ins id='p95Vw'></ins><ul id='p95Vw'></ul><sub id='p95Vw'></sub></form><legend id='p95Vw'></legend><bdo id='p95Vw'><pre id='p95Vw'><center id='p95Vw'></center></pre></bdo></b><th id='p95Vw'></th></span></q></dt></tr></i><div id='p95Vw'><tfoot id='p95Vw'></tfoot><dl id='p95Vw'><fieldset id='p95Vw'></fieldset></dl></div>
      2. <tfoot id='p95Vw'></tfoot>
          <bdo id='p95Vw'></bdo><ul id='p95Vw'></ul>

                    <tbody id='p95Vw'></tbody>
                1. 本文介绍了如何处理重复条目的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 PHP 表单,可以将数据输入到我的 MySQL 数据库中.我的主键是用户输入的值之一.当用户输入表中已存在的值时,MySQL 错误Duplicate entry 'entered value' for key 1"被退回.而不是那个错误,我想提醒用户他们需要输入不同的值.只是一个回显的消息或其他东西.

                  I have a PHP form that enters data into my MySQL database. My primary key is one of the user-entered values. When the user enters a value that already exists in the table, the MySQL error "Duplicate entry 'entered value' for key 1" is returned. Instead of that error, I would like to alert the user that they need to enter a different value. Just an echoed message or something.

                  如何将特定的 MySQL 错误转换为 PHP 消息?

                  How to turn a specific MySQL error into a PHP message?

                  推荐答案

                  要检查此特定错误,您需要找到 错误代码.重复键是1062.然后使用 errno() 比较:

                  To check for this specific error, you need to find the error code. It is 1062 for duplicate key. Then use the result from errno() to compare with:

                  mysqli_query('INSERT INTO ...');
                  if (mysqli_errno() == 1062) {
                      print 'no way!';
                  }
                  

                  关于编程风格的说明
                  您应该始终设法避免使用幻数(我知道,我是在这个答案中介绍它的人).相反,您可以将已知错误代码 (1062) 分配给一个常量(例如 MYSQLI_CODE_DUPLICATE_KEY).这将使您的代码更易于维护,因为 if 语句中的条件在 1062 的含义从记忆中消失后的几个月内仍然可读:)

                  A note on programming style
                  You should always seek to avoid the use of magic numbers (I know, I was the one to introduce it in this answer). Instead, you could assign the known error code (1062) to a constant (e.g. MYSQLI_CODE_DUPLICATE_KEY). This will make your code easier to maintain as the condition in the if statement is still readable in a few months when the meaning of 1062 has faded from memory :)

                  这篇关于如何处理重复条目的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 register_shutdown_function() 处理 PHP 中的致命错误 下一篇:使用 file_get_contents 进行良好的错误处理

                  相关文章

                  最新文章

                  <legend id='8KcXX'><style id='8KcXX'><dir id='8KcXX'><q id='8KcXX'></q></dir></style></legend>

                    <i id='8KcXX'><tr id='8KcXX'><dt id='8KcXX'><q id='8KcXX'><span id='8KcXX'><b id='8KcXX'><form id='8KcXX'><ins id='8KcXX'></ins><ul id='8KcXX'></ul><sub id='8KcXX'></sub></form><legend id='8KcXX'></legend><bdo id='8KcXX'><pre id='8KcXX'><center id='8KcXX'></center></pre></bdo></b><th id='8KcXX'></th></span></q></dt></tr></i><div id='8KcXX'><tfoot id='8KcXX'></tfoot><dl id='8KcXX'><fieldset id='8KcXX'></fieldset></dl></div>

                  1. <small id='8KcXX'></small><noframes id='8KcXX'>

                      <bdo id='8KcXX'></bdo><ul id='8KcXX'></ul>
                    <tfoot id='8KcXX'></tfoot>