• <small id='ZJzmz'></small><noframes id='ZJzmz'>

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

          <bdo id='ZJzmz'></bdo><ul id='ZJzmz'></ul>

        插入前检查数据库中是否存在行

        时间:2023-09-19
              <tfoot id='0frRz'></tfoot>

                <bdo id='0frRz'></bdo><ul id='0frRz'></ul>
              • <legend id='0frRz'><style id='0frRz'><dir id='0frRz'><q id='0frRz'></q></dir></style></legend>
              • <small id='0frRz'></small><noframes id='0frRz'>

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

                    <tbody id='0frRz'></tbody>
                  本文介绍了插入前检查数据库中是否存在行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  $DBH = new PDO($dsn, $username, $password, $opt);
                  
                  $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                  $DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
                  
                  $STH = $DBH->prepare("INSERT INTO requests (id,imdbid,msg) VALUES ('',:imdbid,:msg)");
                  $STH->bindParam(':imdbid', $_POST['imdbid']);
                  $STH->bindParam(':msg', $_POST['msg']);
                  
                  $STH->execute();
                  echo "<p>Successfully Requested ".$_POST['imdbid']."! Thanks!</p>";
                  

                  是否有一些 SQL 查询将检查和插入或什么?我需要它来检查用户输入的内容是否已经在数据库中,所以如果用户输入的 imdbid 已经存在,那么它就不会继续插入任何内容.我该怎么做?我知道我可以做一个 fetch_all 并为它做一个 foreach 但这不是只有在你执行后才有效吗?

                  Is there either some SQL Query that will check and insert or what? I need it to check if whatever the user typed is already in the db so if the user typed in a imdbid that is already there then it wont continue inserting anything. How would I do this? I know I can do a fetch_all and make a foreach for it but doesnt that only work after you execute?

                  推荐答案

                  最好在列上设置约束以防止重复数据,而不是检查和插入.

                  It's better to set a constraint on your columns to prevent duplicate data instead of checking and inserting.

                  只需在 imdbid 上设置一个 UNIQUE 约束:

                  Just set a UNIQUE constraint on imdbid:

                  ALTER TABLE `requests` ADD UNIQUE `imdbid_unique`(`imdbid`);
                  

                  这样做的原因是您不会遇到竞争条件.

                  The reason for doing this is so that you don't run into a race condition.

                  在完成检查和实际插入数据之间有一个小窗口,在那个小窗口中,可能插入的数据会与要插入的数据发生冲突.

                  There's a small window between finishing the check, and actually inserting the data, and in that small window, data could be inserted that will conflict with the to-be-inserted data.

                  解决方案?使用约束并检查 $DBH->error() 是否存在插入错误.如果有任何错误,您就知道存在重复项,然后您可以通知您的用户.

                  Solution? Use constraints and check $DBH->error() for insertion errors. If there are any errors, you know that there's a duplicate and you can notify your user then.

                  我注意到你正在使用这个,$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);.在这种情况下,您不需要检查 ->error() 因为 PDO 会抛出异常.只需像这样用 try 和 catch 包裹你的执行:

                  I noticed that you are using this, $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);. In this case, you don't need to check ->error() because PDO will throw an exception. Just wrap your execute with try and catch like this:

                  $duplicate = false;
                  
                  try {
                      $STH->execute();
                  } catch (Exception $e) {
                      echo "<p>Failed to Request ".$_POST['imdbid']."!</p>";
                      $duplicate = true;
                  }
                  
                  if (!$duplicate)
                      echo "<p>Successfully Requested ".$_POST['imdbid']."! Thanks!</p>";
                  

                  这篇关于插入前检查数据库中是否存在行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PDO 返回所有行 下一篇:PDO 返回不正确但重复的数据.密钥不在数据库中

                  相关文章

                  最新文章

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

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

                  <tfoot id='utNhF'></tfoot>

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

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