Maison  >  Questions et réponses  >  le corps du texte

Si le tableau n'existe pas, insérez dans le tableau

<p>J'ai une application dans laquelle l'utilisateur peut ajouter des numéros de série (unités) à un circuit. J'essaie de modifier une requête d'insertion pour deux tables afin de vérifier si un ID de cellule existe déjà. S'il n'existe pas, je souhaite l'insérer, mais s'il existe, je ne souhaite pas insérer de nouvel enregistrement. J'ai cherché et essayé d'appliquer les réponses que j'ai trouvées sur SO à ce sujet sans aucun succès.</p> <p>这是我的代码,以 控制器</p> <pre class="brush:php;toolbar:false;">fonction publique AddNewCell() { si ($_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); si (vide ($ stageCheckId)) { echo json_encode("0"); } autre { $cellId = $this->form->InsertNewCell($circuitId, $stageCheckId, $toteId, $posId); $this->wk->InsertCell($circuitId, $cellId, $cellNum, $toteId, $posId); echo json_encode($cellId); } } }</pré> <p>编队模型</p> <pre class="brush:php;toolbar:false;">fonction publique 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->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; }≪/pré> <p>工作表模型</p> <pre class="brush:php;toolbar:false;">fonction publique InsertCell($circuitId, $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(); }</pré> <p> <code>$this->db->query("更改表 tbl_Cell_Tote_Track 添加唯一的 (Cell_Id);</code> 到模型函数,但当使用现有序列号输入单元格时仍然收到重复项。我也尝试过</p> <pre class="brush:php;toolbar:false;">fonction publique 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 OÙ N'EXISTE PAS (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; }</pré> <p> Et le fichier php.多代码,请告诉我。</p>
P粉546138344P粉546138344383 Il y a quelques jours551

répondre à tous(1)je répondrai

  • P粉436688931

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

    Si vous êtes en select 语句上设置 where vous pouvez sélectionner le dernier code (avec select) comme

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

    Et modifiez le paramètre de fonction d’envoi de l’ID de l’unité.

    Si Cell_Id est auto-incrémenté, alors vous devez définir la contrainte avec une colonne différente.

    répondre
    0
  • Annulerrépondre