使用PHP从html表单/表格提交动态行数到MySQL

时间:2015-05-28 13:57:58

标签: javascript php jquery html mysql

所以我在堆栈溢出的帮助下写了以下内容。它的目标是获取具有4个值的动态行数,并通过单个提交按钮将所有行插入到mysql表中。

以下代码有效,但只插入最后一行。任何帮助表示赞赏谢谢! -Todd



<html>
<head>
<script>
function deleteRow(row)
{
    var i=row.parentNode.parentNode.rowIndex;
    document.getElementById('FIRST').deleteRow(i);
}


function insRow()
{
    console.log( 'hi');
    var x=document.getElementById('FIRST');
    var new_row = x.rows[1].cloneNode(true);
    var len = x.rows.length;
    new_row.cells[0].innerHTML = len;

    var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
    inp1.id += len;
    inp1.value = '';
    var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
    inp2.id += len;
    inp2.value = '';
    var inp3 = new_row.cells[3].getElementsByTagName('input')[0];
	    inp3.id += len;
    inp3.value = '';
    var inp4 = new_row.cells[4].getElementsByTagName('input')[0];
	    inp4.id += len;
    inp4.value = '';
    x.appendChild( new_row );

} </script>
</head>
<body>

<form action="insertform.php" method = "post">
<table id="FIRST" border="1">
        <tr>
			<td>Line</td>
            <td>Value</td>
            <td>Selector</td>
            <td>Start Frequency</td>
            <td>Stop Frequency</td>
            <!--<td>Delete?</td>
            <td>Add Rows?</td> -->
        </tr>
        <tr>
        <td>1</td>
            <td><input  type="number"  name = "Value1"/></td>
            <td><input type="text" list="Selector" name="Selector" />
            	<datalist id="Selector" >
            	<option value=">">
            	<option value="<">
            	<option value="=">
            	</datalist>
            	</td>
            <td><input  type="number"  name="StartFreq"/></td>
            <td><input type="number" name="StopFreq"/></td>
            </tr>
    </table>
    <td><input type="button" id="addmoreFIRSTbutton" value="Add More Lines" onclick="insRow()"/></td>
    <input type = "submit" name ="submit">
</form>



<?php
if(isset($_POST['submit'])){

$servername = "localhost";
$username = "root";
$password = "mellow";
$dbname = "yellow";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}



$sql = "INSERT INTO FIRST ( Value, Selector, StartFreq, StopFreq)
VALUES ('$_POST[Value1]','$_POST[Selector]','$_POST[StartFreq]','$_POST[StopFreq]')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

}
?>
</body>
</html>
&#13;
&#13;
&#13;

我已经在查询中添加了以下内容并按照建议添加了[]但我仍然需要帮助将数组转换为可用的PHP / SQL语句。

见下文

&#13;
&#13;
 if($_POST[submit])
    {
     foreach ($_POST['Line'] as $key => $value)
        {
            $Line = $_POST["Line"][$key];
            $Value = $_POST["Value"][$key];
            $Selector = $_POST["Selector"][$key];
            $StartFreq = $_POST["StartFreq"][$key];
            $StopFreq = $_POST["StopFreq"][$key];

            $sql = mysql_query("insert into FIRST values ('$Line', '$Value','$Selector', '$StartFreq', '$StopFreq')");
        }

    }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

正如Charles所说,将[]放在每个输入字段名称的末尾,以便后端的php代码将它们视为数组。然后,在您的PHP代码中,使用foreach循环迭代数组。在foreach循环中,动态构造SQL INSERT语句并执行它们,每行一个。 。这就是我一直这样做的方式。