pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

        <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="dvjpxvv" id='0frRz'><tfoot id='0frRz'></tfoot><dl id='0frRz'><fieldset id='0frRz'></fieldset></dl></div>

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

        Check if row exists in the database before inserting(插入前檢查數(shù)據(jù)庫中是否存在行)
      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="dppplj5" 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ù)庫中是否存在行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(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ù)庫中,所以如果用戶輸入的 imdbid 已經(jīng)存在,那么它就不會繼續(xù)插入任何內(nèi)容.我該怎么做?我知道我可以做一個 fetch_all 并為它做一個 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è)置一個 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.

                  在完成檢查和實(shí)際插入數(shù)據(jù)之間有一個小窗口,在那個小窗口中,可能插入的數(shù)據(jù)會與要插入的數(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() 是否存在插入錯誤.如果有任何錯誤,您就知道存在重復(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.

                  我注意到你正在使用這個,$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);.在這種情況下,您不需要檢查 ->error() 因?yàn)?PDO 會拋出異常.只需像這樣用 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ù)庫中是否存在行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務(wù)器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數(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="tvxjt7p" 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. 主站蜘蛛池模板: 兰州牛肉面加盟,兰州牛肉拉面加盟-京穆兰牛肉面 | sus630/303cu不锈钢棒,440C/430F/17-4ph不锈钢研磨棒-江苏德镍金属科技有限公司 | 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 玉米加工设备,玉米深加工机械,玉米糁加工设备.玉米脱皮制糁机 华豫万通粮机 | 拖链电缆_柔性电缆_伺服电缆_坦克链电缆-深圳市顺电工业电缆有限公司 | 「安徽双凯」自动售货机-无人售货机-成人用品-自动饮料食品零食售货机 | 温州在线网| 波纹补偿器_不锈钢波纹补偿器_巩义市润达管道设备制造有限公司 | 网站制作优化_网站SEO推广解决方案-无锡首宸信息科技公司 | 厂房出租-厂房规划-食品技术-厂房设计-厂房装修-建筑施工-设备供应-设备求购-龙爪豆食品行业平台 | 篷房[仓储-婚庆-展览-活动]生产厂家-江苏正德装配式帐篷有限公司 | 保镖公司-私人保镖-深圳保镖公司【环宇兄弟保镖】 | 主题班会网 - 安全教育主题班会,各类主题班会PPT模板 | 电竞学校_电子竞技培训学校学院-梦竞未来电竞学校官网 | 不锈钢/气体/液体玻璃转子流量计(防腐,选型,规格)-常州天晟热工仪表有限公司【官网】 | 净化车间_洁净厂房_净化公司_净化厂房_无尘室工程_洁净工程装修|改造|施工-深圳净化公司 | 莱州网络公司|莱州网站建设|莱州网站优化|莱州阿里巴巴-莱州唯佳网络科技有限公司 | 京港视通报道-质量走进大江南北-京港视通传媒[北京]有限公司 | 三价铬_环保铬_环保电镀_东莞共盈新材料贸易有限公司 | 海外仓系统|国际货代系统|退货换标系统|WMS仓储系统|海豚云 | 新中天检测有限公司青岛分公司-山东|菏泽|济南|潍坊|泰安防雷检测验收 | 大型低温冷却液循环泵-低温水槽冷阱「厂家品牌」京华仪器_京华仪器 | 深圳市索富通实业有限公司-可燃气体报警器 | 可燃气体探测器 | 气体检测仪 | 电子海图系统-电梯检验系统-智慧供热系统开发-商品房预售资金监管系统 | 台式低速离心机-脱泡离心机-菌种摇床-常州市万丰仪器制造有限公司 | 篮球架_乒乓球台_足球门_校园_竞技体育器材_厂家_价格-沧州浩然体育器材有限公司 | 纯化水设备-纯水设备-超纯水设备-[大鹏水处理]纯水设备一站式服务商-东莞市大鹏水处理科技有限公司 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 杭州双螺杆挤出机-百科| 北京网站建设|北京网站开发|北京网站设计|高端做网站公司 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | 深圳高新投三江工业消防解决方案提供厂家_服务商_园区智慧消防_储能消防解决方案服务商_高新投三江 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 三佳互联一站式网站建设服务|网站开发|网站设计|网站搭建服务商 赛默飞Thermo veritiproPCR仪|ProFlex3 x 32PCR系统|Countess3细胞计数仪|371|3111二氧化碳培养箱|Mirco17R|Mirco21R离心机|仟诺生物 | 接地电阻测试仪[厂家直销]_电缆故障测试仪[精准定位]_耐压测试仪-武汉南电至诚电力设备 | 建筑工程资质合作-工程资质加盟分公司-建筑资质加盟 | 国产离子色谱仪,红外分光测油仪,自动烟尘烟气测试仪-青岛埃仑通用科技有限公司 | 上海办公室装修,写字楼装修—启鸣装饰设计工程有限公司 |