Heim  >  Fragen und Antworten  >  Hauptteil

Wenn die Tabelle nicht vorhanden ist, fügen Sie sie in die Tabelle ein

<p>Ich habe eine Anwendung, bei der der Benutzer einer Schaltung Seriennummern (Einheiten) hinzufügen kann. Ich versuche, eine Einfügeabfrage für zwei Tabellen zu ändern, um zu prüfen, ob bereits eine Zellen-ID vorhanden ist. Wenn es nicht existiert, möchte ich es einfügen, aber wenn es existiert, möchte ich keinen neuen Datensatz einfügen. Ich habe die Antworten, die ich zu diesem Thema auf SO gefunden habe, ohne Erfolg gesucht und angewendet.</p> <p>这是我的代码,以 控制器</p> <pre class="brush:php;toolbar:false;">öffentliche Funktion AddNewCell() { if ($_SERVER['REQUEST_METHOD'] == 'POST') { $CircuitId = $_POST["CircuitId"]; $cellNum = filter_var($_POST["cellNum"], FILTER_SANITIZE_STRING); $toteId = $_POST["toteId"]; $posId = $_POST["posId"]; $stageCheckId = $this->GetStageIdByBatId($cellNum); if (empty($stageCheckId)) { echo json_encode("0"); } anders { $cellId = $this->form->InsertNewCell($schaltungId, $stageCheckId, $toteId, $posId); $this->wk->InsertCell($schaltungId, $cellId, $cellNum, $toteId, $posId); echo json_encode($cellId); } } }</pre> <p>编队模型</p> <pre class="brush:php;toolbar:false;">öffentliche Funktion InsertNewCell($schaltungId, $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->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>工作表模型</p> <pre class="brush:php;toolbar:false;">öffentliche Funktion InsertCell($schaltungId, $cellId, $cellNum, $toteId, $posId) { $this->db->query("SELECT Circuit_Num FROM tbl_Circuit_Track 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->execute(); }</pre> <p>我尝试通过添加在表的 Cell_Id 列上添加唯一约束 <code>$this->db->query("更改表 tbl_Cell_Tote_Track 添加唯一的 (Cell_Id);</code> 到模型函数,但当使用现有序列号输入单元格时仍然收到重复项.我也尝试过</p> <pre class="brush:php;toolbar:false;">öffentliche Funktion InsertNewCell($schaltungId, $stageCheckId, $toteId, $posId) { $this->db->query("INSERT INTO tbl_Cell_Tote_Track (Circuit_Id, Stage_Check_Id, Tote_Id, Position_Id) SELECT $schaltungId, $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>这似乎可以防止该表出现重复和 php 的新手.任何帮助是极大的赞赏。如果需要包含更多代码,请告诉我.</p>
P粉546138344P粉546138344383 Tage vor546

Antworte allen(1)Ich werde antworten

  • P粉436688931

    P粉4366889312023-09-06 15:11:58

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

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

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

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

    Antwort
    0
  • StornierenAntwort