gpt4 book ai didi

javascript - 如何防止 foreach 复制 HTML 表数组中的条目

转载 作者:行者123 更新时间:2023-11-30 22:02:42 29 4
gpt4 key购买 nike

我正在尝试将从 HTML 数组表收集的多个条目添加到数据库,但数量的值似乎是重复的,包括备注的值。整个条目似乎也在重复。我怎样才能防止它们发生?

这是我将表格插入数据库的 PHP 代码:

<?php
$tmp = 0;
if (isset($_POST['submit'])) {
if(!empty($_POST["inRemarks"])) {
foreach($_POST["inRemarks"] as $inRemarks) if ($tmp++ < 2) {
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$inQty = $_POST['incQty'];
foreach ($inQty as $key => $incQty) {
$prod = $_POST['prodItem'];
foreach($prod as $prodItem) {
$emp = $_POST['emp'];
$emp1 = $conn->query("SELECT empID AS empA FROM employee WHERE empName = '$emp'");
$emp2 = $emp1->fetch(PDO::FETCH_ASSOC);
$emp3 = $emp2['empA'];
$prod1 = $conn->query("SELECT prodID AS prodA FROM product WHERE prodName = '$prodItem'");
$prod2 = $prod1->fetch(PDO::FETCH_ASSOC);
$prod3 = $prod2['prodA'];
$sql = "INSERT INTO incoming (inQty, inDate, receiptNo, inRemarks, empID, prodID)
VALUES ('$incQty',CURDATE(),'".$_POST['rcno']."','$inRemarks','$emp3','$prod3')";
$result = $conn->query($sql);
echo "<meta http-equiv='refresh' content='0'>";
}
}
}
}
}
?>

这是我的表单和表格的 JavaScript 代码:

function validateForm() {
if(document.getElementById('addRcpt').value == "") {
alert('Please Enter Receipt Number');
document.getElementById('addRcpt').style.borderColor = "red";
return false;
}
if (document.getElementById('addQty').value == "") {
alert('Please Enter Quantity');
document.getElementById('addQty').style.borderColor = "red";
return false;
}
if(confirm('Are you sure you want to add this entry?')) {
alert("Incoming Product Successfully Added");
return true;
}
else {
return false;
}
}

function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var current = '';
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {
alert("Cannot delete all the rows.");
break;
}

table.deleteRow(i);
rowCount--;
i--;
regroup(i,rowCount,tableID);
}
}
}catch(e) {
alert(e);
}
}

function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
if(i==1){
newcell.innerHTML = (rowCount+1)
}
else{
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value="";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}

function regroup(i,rc,ti){
for(j = (i+1);j<rc;j++){
document.getElementById(ti).rows[j].cells[1].innerHTML = j+1;
}
}

查询结果:

Query result

最佳答案

嵌套的 foreach 循环使您的查询成倍增加,而您的代码完全没有按照您的预期行事。

<?php

if (isset($_POST['submit'])) {

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

for ($index = 0; $index < count($_POST['prodItem']; $index++) {

$inRemarks = $_POST["inRemarks"][$index];
$prodItem = $_POST['prodItem'][$index];
$inQty = $_POST['incQty'][$index];
$emp = $_POST['emp'];

$emp1 = $conn->query("SELECT empID AS empA FROM employee WHERE empName = '$emp'");
$emp2 = $emp1->fetch(PDO::FETCH_ASSOC);
$emp3 = $emp2['empA'];
$prod1 = $conn->query("SELECT prodID AS prodA FROM product WHERE prodName = '$prodItem'");
$prod2 = $prod1->fetch(PDO::FETCH_ASSOC);
$prod3 = $prod2['prodA'];
$sql = "INSERT INTO incoming (inQty, inDate, receiptNo, inRemarks, empID, prodID)
VALUES ('$incQty',CURDATE(),'".$_POST['rcno']."','$inRemarks','$emp3','$prod3')";
$result = $conn->query($sql);

echo "<meta http-equiv='refresh' content='0'>";
}
}
?>

关于javascript - 如何防止 foreach 复制 HTML 表数组中的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42888761/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com