久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

        <tfoot id='0frRz'></tfoot>

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

      3. <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 class="qwawimqqmiuu" id='0frRz'><tfoot id='0frRz'></tfoot><dl id='0frRz'><fieldset id='0frRz'></fieldset></dl></div>

        插入前檢查數(shù)據(jù)庫(kù)中是否存在行

        Check if row exists in the database before inserting(插入前檢查數(shù)據(jù)庫(kù)中是否存在行)
      4. <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 class="qwawimqqmiuu" id='ZJzmz'><tfoot id='ZJzmz'></tfoot><dl id='ZJzmz'><fieldset id='ZJzmz'></fieldset></dl></div>

                  <bdo id='ZJzmz'></bdo><ul id='ZJzmz'></ul>
                    <tbody id='ZJzmz'></tbody>
                  本文介紹了插入前檢查數(shù)據(jù)庫(kù)中是否存在行的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  $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 查詢將檢查和插入或什么?我需要它來檢查用戶輸入的內(nèi)容是否已經(jīng)在數(shù)據(jù)庫(kù)中,所以如果用戶輸入的 imdbid 已經(jīng)存在,那么它就不會(huì)繼續(xù)插入任何內(nèi)容.我該怎么做?我知道我可以做一個(gè) fetch_all 并為它做一個(gè) foreach 但這不是只有在你執(zhí)行后才有效嗎?

                  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?

                  推薦答案

                  最好在列上設(shè)置約束以防止重復(fù)數(shù)據(jù),而不是檢查和插入.

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

                  只需在 imdbid 上設(shè)置一個(gè) UNIQUE 約束:

                  Just set a UNIQUE constraint on imdbid:

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

                  這樣做的原因是您不會(huì)遇到競(jìng)爭(zhēng)條件.

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

                  在完成檢查和實(shí)際插入數(shù)據(jù)之間有一個(gè)小窗口,在那個(gè)小窗口中,可能插入的數(shù)據(jù)會(huì)與要插入的數(shù)據(jù)發(fā)生沖突.

                  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() 是否存在插入錯(cuò)誤.如果有任何錯(cuò)誤,您就知道存在重復(fù)項(xiàng),然后您可以通知您的用戶.

                  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.

                  我注意到你正在使用這個(gè),$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);.在這種情況下,您不需要檢查 ->error() 因?yàn)?PDO 會(huì)拋出異常.只需像這樣用 try 和 catch 包裹你的執(zhí)行:

                  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>";
                  

                  這篇關(guān)于插入前檢查數(shù)據(jù)庫(kù)中是否存在行的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語(yǔ)句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個(gè)服務(wù)器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識(shí)別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個(gè)參數(shù))
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結(jié)果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問被拒絕)
                      <tbody id='utNhF'></tbody>
                        <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 class="qwawimqqmiuu" 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>
                          2. 主站蜘蛛池模板: 欧美精品v国产精品v日韩精品 | 亚洲性人人天天夜夜摸 | 99热欧美| 亚洲黄色av | 国精产品一区二区三区 | 99精品国产成人一区二区 | 狠狠的日 | 99热在线免费| 国产精品黄视频 | 国产一区 日韩 | 奇米超碰在线 | 91久久精品一区二区二区 | 精品国产18久久久久久二百 | 91视频免费视频 | 国产999精品久久久久久 | 99久久精品国产麻豆演员表 | www.99re | 久久久久久久久久爱 | 91一区二区三区 | 国产精品精品视频一区二区三区 | av第一页| 亚洲国产一区视频 | 男人的天堂视频网站 | 精品国产欧美一区二区 | 国产日韩欧美中文字幕 | 精品久久久久久久 | 欧美日韩在线高清 | 伊人伊成久久人综合网站 | 黄色片免费看视频 | 国产精品视频网 | 天天弄| 黄色精品 | 中文字幕乱码一区二区三区 | 一级免费在线视频 | 欧美精品一二区 | 老外黄色一级片 | 国产精品久久久久久久久久免费 | 亚洲精品视频一区 | 欧美成人a∨高清免费观看 老司机午夜性大片 | 久久久精彩视频 | 日韩成人免费在线视频 |