如果表不存在则插入到表中
P粉546138344
P粉546138344 2023-09-05 15:53:43
[PHP讨论组]
<p>我有一个应用程序,用户可以在其中向电路添加序列号(单元)。我正在尝试修改两个表的插入查询以检查单元格 ID 是否已存在。如果不存在,我想插入它,但如果它确实存在,我不想插入新记录。我已经搜索并尝试应用我在 SO 上找到的与此相关的答案,但没有取得任何成功。</p> <p>这是我的代码,以 控制器</p> <pre class="brush:php;toolbar:false;">public function AddNewCell() { if ($_SERVER['REQUEST_METHOD'] == 'POST') { $circuitId = $_POST[&quot;circuitId&quot;]; $cellNum = filter_var($_POST[&quot;cellNum&quot;], FILTER_SANITIZE_STRING); $toteId = $_POST[&quot;toteId&quot;]; $posId = $_POST[&quot;posId&quot;]; $stageCheckId = $this-&gt;GetStageIdByBatId($cellNum); if (empty($stageCheckId)) { echo json_encode(&quot;0&quot;); } else { $cellId = $this-&gt;form-&gt;InsertNewCell($circuitId, $stageCheckId, $toteId, $posId); $this-&gt;wk-&gt;InsertCell($circuitId, $cellId, $cellNum, $toteId, $posId); echo json_encode($cellId); } } }</pre> <p>编队模型</p> <pre class="brush:php;toolbar:false;">public function InsertNewCell($circuitId, $stageCheckId, $toteId, $posId) { $this-&gt;db-&gt;query(&quot;INSERT INTO tbl_Cell_Tote_Track (Circuit_Id, Stage_Check_Id, Tote_Id, Position_Id) VALUES (:cid, :scid, :tid, :pid)&quot;); $this-&gt;db-&gt;bind(&quot;:cid&quot;, $circuitId); $this-&gt;db-&gt;bind(&quot;:scid&quot;, $stageCheckId); $this-&gt;db-&gt;bind(&quot;:tid&quot;, $toteId); $this-&gt;db-&gt;bind(&quot;:pid&quot;, $posId); $this-&gt;db-&gt;execute(); $this-&gt;db-&gt;query(&quot;SELECT TOP(1) Cell_Id FROM tbl_Cell_Tote_Track ORDER BY Cell_Id DESC&quot;); return $this-&gt;db-&gt;single()-&gt;Cell_Id; }</pre> <p>工作表模型</p> <pre class="brush:php;toolbar:false;">public function InsertCell($circuitId, $cellId, $cellNum, $toteId, $posId) { $this-&gt;db-&gt;query(&quot;SELECT Circuit_Num FROM tbl_Circuit_Track WHERE Circuit_Id = :cid&quot;); $this-&gt;db-&gt;bind(&quot;:cid&quot;, $circuitId); $circuitNum = $this-&gt;db-&gt;single()-&gt;Circuit_Num; $position = $this-&gt;GetCellPos($toteId, $posId); $this-&gt;db-&gt;query(&quot;INSERT INTO tbl_OCV_Worksheet (Cell_Id, Circuit_Id, Circuit_Num, Position_Num, Serial_Num) VALUES (:clid, :cirid, :cn, :pn, :cnum)&quot;); $this-&gt;db-&gt;bind(&quot;:clid&quot;, $cellId); $this-&gt;db-&gt;bind(&quot;:cirid&quot;, $circuitId); $this-&gt;db-&gt;bind(&quot;:cn&quot;, $circuitNum); $this-&gt;db-&gt;bind(&quot;:pn&quot;, $position); $this-&gt;db-&gt;bind(&quot;:cnum&quot;, $cellNum); $this-&gt;db-&gt;execute(); }</pre> <p>我尝试通过添加在表的 Cell_Id 列上添加唯一约束 <code>$this->db->query("更改表 tbl_Cell_Tote_Track 添加唯一的 (Cell_Id);</code> 到模型函数,但当使用现有序列号输入单元格时仍然收到重复项。我也尝试过</p> <pre class="brush:php;toolbar:false;">public function InsertNewCell($circuitId, $stageCheckId, $toteId, $posId) { $this-&gt;db-&gt;query(&quot;INSERT INTO tbl_Cell_Tote_Track (Circuit_Id, Stage_Check_Id, Tote_Id, Position_Id) SELECT $circuitId, $stageCheckId, $toteId, $posId WHERE NOT EXISTS(SELECT Cell_Id FROM tbl_Cell_Tote_Track)&quot;); $this-&gt;db-&gt;execute(); $this-&gt;db-&gt;query(&quot;SELECT TOP(1) Cell_Id FROM tbl_Cell_Tote_Track ORDER BY Cell_Id DESC&quot;); return $this-&gt;db-&gt;single()-&gt;Cell_Id; }</pre> <p>这似乎可以防止该表出现重复,但一位高级同事告诉我这是不正确的。提前道歉,因为我是 SQL 和 php 的新手。任何帮助是极大的赞赏。如果需要包含更多代码,请告诉我。</p>
P粉546138344
P粉546138344

全部回复(1)
P粉436688931

如果您在 select 语句上设置 where,则可以选择最后一个代码(带有 select),例如

"NOT EXISTS (SELECT Cell_Id FROM tbl_Cell_Tote_Track WHERE Cell_id = $cellId)"

并更改发送单元 ID 的函数参数。

如果 Cell_Id 是自动增量,那么您需要使用不同的列定义该约束。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号