如何防止foreach从HTML表数组中复制条目

时间:2017-03-19 16:01:27

标签: javascript php html mysql

我正在尝试将从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

3 个答案:

答案 0 :(得分:0)

使用array_unique

过滤您的 1 => 2 - 1 => sequence length 1 unset first bit: b11 => b10 => seen bit configuration b10 0 => 2 - 0 => sequence length 2, unset second bit: b10 => b00 => seen bit configuration b00 $_POST["inRemarks"]
$_POST['incQty'];

答案 1 :(得分:0)

你必须在第一个foreach循环中移动 $ sql =&#34; INSERT INTO ...

foreach($_POST["inRemarks"] as $inRemarks){
.........other foreach loops and rest of code.........
$sql = "INSERT INTO...
}

答案 2 :(得分:0)

嵌套的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'>";
        }
    }
?>