如果表不存在則插入到表中
P粉546138344
P粉546138344 2023-09-05 15:53:43
0
1
429
<p>我有一個應用程序,用戶可以在其中向電路添加序號(單元)。我正在嘗試修改兩個表的插入查詢以檢查單元格 ID 是否已存在。如果不存在,我想插入它,但如果它確實存在,我不想插入新記錄。我已經搜索並嘗試應用我在 SO 上找到的與此相關的答案,但沒有任何成功。</p> <p>這是我的程式碼,以 控制器</p>
公用函數AddNewCell()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $電路ID = $_POST[“電路ID”];
            $cellNum = filter_var($_POST["cellNum"], FILTER_SANITIZE_STRING);
            $toteId = $_POST[“toteId”];
            $posId = $_POST[“posId”];

            $stageCheckId = $this->GetStageIdByBatId($cellNum);
            如果(空($stageCheckId)){
                迴聲 json_encode(“0”);
            } 別的 {
                $cellId = $this->form->InsertNewCell($CircuitId, $stageCheckId, $toteId, $posId);
                $this->wk->InsertCell($CircuitId, $cellId, $cellNum, $toteId, $posId);
                迴聲 json_encode($cellId);
            }
        }
    }</pre>
<p>編隊模型</p>
<pre class="brush:php;toolbar:false;">公用函數InsertNewCell($CircuitId, $stageCheckId, $toteId, $posId)
    {
        $this->db->query("INSERT INTO tbl_Cell_Tote_Track (Circuit_Id, Stage_Check_Id, Tote_Id, Position_Id) VALUES (:cid, :scid, :tid, :pid)");
        $this->db->bind(":cid", $CircuitId);
        $this->db->bind(":scid", $stageCheckId);
        $this->db->bind(":tid", $toteId);
        $this->db->bind(":pid", $posId);
        $this->db->e​​xecute();

        $this->db->query("SELECT TOP(1) Cell_Id FROM tbl_Cell_Tote_Track ORDER BY Cell_Id DESC");
        return $this->db->single()->Cell_Id;
    }</pre>
<p>工作表模型db->query("從 tbl_Circuit_Track 選擇 Circuit_Num WHERE Circuit_Id = :cid");
        $this->db->bind(":cid", $CircuitId);
        $CircuitNum = $this->db->single()->Circuit_Num;

        $position = $this->GetCellPos($toteId, $posId);

        $this->db->query("INSERT INTO tbl_OCV_Worksheet (Cell_Id, Circuit_Id, Circuit_Num, Position_Num, Serial_Num) VALUES (:clid, :cirid, :cn, :pn, :cnum)");
        $this->db->bind(":clid", $cellId);
        $this->db->bind(":cirid", $CircuitId);
        $this->db->bind(":cn", $CircuitNum);
        $this->db->bind(":pn", $position);
        $this->db->bind(":cnum", $cellNum);
        $this->db->e​​xecute();
    }</pre>
<p>我嘗試透過在表中新增 Cell_Id 列上新增唯一約束
$this->db->query("更改表 tbl_Cell_Tote_Track 新增唯一的 (Cell_Id);;
到模型函數,但當使用現有序號輸入儲存格時仍會收到重複項。我也嘗試過</p>
<pre class="brush:php;toolbar:false;">public function InsertNewCell($circuitId, $stageCheckId, $toteId, $posId)
{
     $this->db->query("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)");
     $this->db->execute();
     $this->db->query("SELECT TOP(1) Cell_Id FROM tbl_Cell_Tote_Track ORDER BY Cell_Id DESC");
     return $this->db->single()->Cell_Id;
}</pre>
<p>這似乎可以防止表格重複,但一位高級同事告訴我這是不正確的。先道歉,因為我是 SQL 和 php 的新手。任何幫助是極大的讚賞。如果需要包含更多程式碼,請告訴我。 </p>            
P粉546138344
P粉546138344

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!