从多行表PHP Javascript

时间:2016-07-22 13:13:23

标签: javascript php html mysql

我在将多个记录添加到MySQL时遇到问题。我知道我错过了一些简单但我无法弄明白的东西。因此,这篇文章......当用户点击提交时,每个事情都在期待这个问题。只识别表格的最后一行。

示例...前2行的单元号为1234-01,1234-02,最后一行的单元号为1234-03。当点击提交时,它会跳过01和02,只进入数据库03.

感谢您的帮助......我的脑袋开始受伤,因为我在桌子上敲打它。

<h2>Please fill in the information below</h2>
<form action="pmUnitCreate.php" method="post">
<p>Click the Add button to add a new row. Click the Delete button to Delete last row.</p>

<table id="myTable">
    <th>Unit Number</th>
    <th>Model Number</th>
    <th>Serial Number</th>
    <th>Scope</th>
    <th><input type="button" id="btnAdd" class="button-add" onClick="insertRow()" value="Add"></input></th>
    <th><input type="button" id="btnDelete" class="button-delete" onClick="deleteRow(-1)" value="Delete"></input></th>
    <tr>
        <td><input type="text" name="UnitID" /></td>
        <td><input type="text" name="ModelNumber" /></td>
        <td><input type="text" name="SerialNumber" /></td>
        <td><select name="Scope">

                <option value="BTank">BTank</option>
                <option value="WSEcon">WSEcon</option>
                <option value="NetPkg">NetPkg</option>
                <option value="CstmCabinet">CstmCabinet</option>
                <option value="CstmCtrl">CstmCtrl</option>
                <option value="CstmRef">CstmRef</option>
                <option value="CstmSM">CstmSM</option>
                <option value="CstmHV">CstmHV</option>
                <option value="CPCtrl">CPCtrl</option>
                <option value="DesiHW">DesiHW</option>
                <option value="DigScroll">DigScroll</option>
                <option value="DFGas">DFGas</option>
                <option value="DWall">DWall</option>
                <option value="MZ-DD">MZ-DD</option>
                <option value="DPP">DPP</option>
                <option value="Encl">Encl</option>
                <option value="PlateHX">PlateHX</option>
                <option value="ERW">ERW</option>

                </select>
                </td>



    </tr>

<br>

<script>

var index = 1;
function insertRow(){
            var table=document.getElementById("myTable");
            var row=table.insertRow(table.rows.length);
            var cell1=row.insertCell(0);
            var t1=document.createElement("input");
                t1.name = "UnitID";
                cell1.appendChild(t1);
            var cell2=row.insertCell(1);
            var t2=document.createElement("input");
                t2.id = "ModelNumber";
                cell2.appendChild(t2);
            var cell3=row.insertCell(2);
            var t3=document.createElement("input");
                t3.id = "SerialNumber";
                cell3.appendChild(t3);
            var cell4=row.insertCell(3);
            var element2 = document.createElement("select");
                element2.id="Scope";
                //element2.type = "select";
    var option1 = document.createElement("option");
        option1.innerHTML = "BTank";
            option1.value = "1";
            element2.add(option1, null);
    var option2 = document.createElement("option");
        option2.innerHTML = "WSEcon";
        option2.value = "2";
        element2.add(option2, null);
    var option3 = document.createElement("option");
        option3.innerHTML = "NetPkg";
            option3.value = "3";
            element2.add(option3, null);
        var option4 = document.createElement("option");
        option4.innerHTML = "CstmCtrl";
            option4.value = "4";
            element2.add(option4, null);
        var option5 = document.createElement("option");
        option5.innerHTML = "CstmRef";
            option5.value = "5";
            element2.add(option5, null);
        var option6 = document.createElement("option");
        option6.innerHTML = "CstmSM";
            option6.value = "6";
            element2.add(option6, null);
        var option7 = document.createElement("option");
        option7.innerHTML = "CstmHV";
            option7.value = "7";
            element2.add(option7, null);        


        cell4.appendChild(element2);

      index++;

}

function deleteRow(){
document.getElementById("myTable").deleteRow(-1);

}

这是SQL代码

<?php
//initialize and read the variables from the posting
//include('Mail.php');
$UnitID=$_POST['UnitID'];
 $UnitID="'" . $UnitID . "'";  //Adds the required quotation marks
$ModelNumber=$_POST['ModelNumber'];
 $ModelNumber="'" . $ModelNumber . "'";  //Adds the required quotation marks
$SerialNumber=$_POST['SerialNumber'];
 $SerialNumber="'" . $SerialNumber . "'";  //Adds the required quotation  marks
$Scope=$_POST['Scope'];
 $Scope="'" . $Scope . "'";  //Adds the required quotation marks

echo $UnitID; 
echo $ModelNumber;
echo $SerialNumber;
echo $Scope;

echo $UnitID;
echo $ModelNumber;
echo $SerialNumber; 
echo $Scope;

echo $UnitID;
echo $ModelNumber;
echo $SerialNumber;
echo $Scope;



//Connect to the database
include('pmconnect.php');

$sql = "INSERT INTO `tblUnitMaster` (`UnitID`, `ModelNumber`,  'SerialNumber`, `Scope`) VALUES ($UnitID, $ModelNumber, $SerialNumber, $Scope )";
$sql = "INSERT INTO `tblUnitMaster` (`UnitID`, `ModelNumber`, `SerialNumber`, `Scope`) VALUES ($UnitID, $ModelNumber, $SerialNumber, $Scope )";
$sql = "INSERT INTO `tblUnitMaster` (`UnitID`, `ModelNumber`, `SerialNumber`, `Scope`) VALUES ($UnitID, $ModelNumber, $SerialNumber, $Scope )";



if ($conn->multi_query($sql) === TRUE)

echo "<h3>Record Created Successfully.</h3><br>";

?>enter code here

3 个答案:

答案 0 :(得分:3)

您正在覆盖$sql (以及其他变量,您正在执行循环吗?) ,因此它只会插入最后一个变量。稍微串联并确保每个查询都有自己的分号将解决问题:

//Connect to the database
include('pmconnect.php');

$sql = "INSERT INTO `tblUnitMaster` (`UnitID`, `ModelNumber`,  'SerialNumber`, `Scope`) VALUES ($UnitID, $ModelNumber, $SerialNumber, $Scope );";
$sql .= "INSERT INTO `tblUnitMaster` (`UnitID`, `ModelNumber`, `SerialNumber`, `Scope`) VALUES ($UnitID, $ModelNumber, $SerialNumber, $Scope );";
$sql .= "INSERT INTO `tblUnitMaster` (`UnitID`, `ModelNumber`, `SerialNumber`, `Scope`) VALUES ($UnitID, $ModelNumber, $SerialNumber, $Scope )";

答案 1 :(得分:0)

这是因为您的代码正在覆盖@SQL变量。您需要使用其他变量名称或尝试插入多行:

Insert table TABLE (A,B)
Values (1,1) ,(2,2),(3,3)

答案 2 :(得分:0)

Image of page

这是带有+索引

的JS的剪辑
<script>

var index = 1;
function insertRow(){
            var table=document.getElementById("myTable");
            var row=table.insertRow(table.rows.length);
            var cell1=row.insertCell(0);
            var t1=document.createElement("input");
                t1.name = "UnitID"+index;
                cell1.appendChild(t1);
            var cell2=row.insertCell(1);
            var t2=document.createElement("input");
                t2.id = "ModelNumber"+index;
                cell2.appendChild(t2);
            var cell3=row.insertCell(2);
            var t3=document.createElement("input");
                t3.id = "SerialNumber"+index;
                cell3.appendChild(t3);
            var cell4=row.insertCell(3);
            var element2 = document.createElement("select");
                element2.id="Scope"+index;
                //element2.type = "select";
    var option1 = document.createElement("option");
        option1.innerHTML = "BTank";
            option1.value = "1";
            element2.add(option1, null);
    var option2 = document.createElement("option");
        option2.innerHTML = "WSEcon";
        option2.value = "2";
        element2.add(option2, null);
    var option3 = document.createElement("option");
        option3.innerHTML = "NetPkg";
            option3.value = "3";
            element2.add(option3, null);
        var option4 = document.createElement("option");
        option4.innerHTML = "CstmCtrl";
            option4.value = "4";
            element2.add(option4, null);
        var option5 = document.createElement("option");
        option5.innerHTML = "CstmRef";
            option5.value = "5";
            element2.add(option5, null);
        var option6 = document.createElement("option");
        option6.innerHTML = "CstmSM";
            option6.value = "6";
            element2.add(option6, null);
        var option7 = document.createElement("option");
        option7.innerHTML = "CstmHV";
            option7.value = "7";
            element2.add(option7, null);        


        cell4.appendChild(element2);

      index++;

}

function deleteRow(){
document.getElementById("myTable").deleteRow(-1);

}

</script>