公共函数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->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;
}</前>
<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->execute();
}</pre>
<p>我尝试通过在表中添加 Cell_Id 列上添加唯一约束
$this->db->query("更改表 tbl_Cell_Tote_Track 添加唯一的 (Cell_Id);
;
到模型函数,但当使用现有序列号输入单元格时仍然收到重复项。我也尝试过</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) 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粉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
是自动增量,那么您需要使用不同的列定义该约束。