早上好!PHP MySQL插入数据库
我正在尝试创建一个登录页面,我们的员工可以将产品添加到我们的发票中。我为您创建了一个测试页面,以便您可以轻松地进行查看,也许可以看到此代码的想法。在页面上,我们的员工可以将产品仅添加到一张发票(因为它是一个测试页面),它是invoice_nr 2014002.我希望能够以一种形式添加多个产品..我使用javascript来添加更多输入字段,但每次运行查询时,它都只会插入最后一行。
这是我的JavaScript代码:
<script type="text/javascript">
function myFunction()
{
var table = document.getElementById("products");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = '<td>Description <input type="text" name="description" value=""></td>';
cell2.innerHTML = '<td>Qty <input type="text" name="qty" value=""></td>';
cell3.innerHTML = '<td>Price <input type="text" name="price" value=""></td>';
cell4.innerHTML = '<td><input type="button" onclick="removeRow(this)" value="Remove"></td>';
}
removeRow = function(el) {
$(el).parents("tr").remove()
}
</script>
这是形式的HTML:
<h1>Add more products to invoice</h1>
<form action="" method="post">
<table id="products">
<tr>
<td>Description <input type="text" name="description" value=""></td>
<td>Qty <input type="text" name="qty" value=""></td>
<td>Price <input type="text" name="price" value=""></td>
</tr>
</table>
<br />
<input type="button" onclick="myFunction()" name="addproduct" value="+ Add product"><br /><br />
<input type="submit" name="addproduct" value="Submit new products">
</form>
而在去年,这是PHP代码..:
require_once("conn.php");
if(isset($_POST['addproduct'])){
$description = addslashes($_POST['description']);
$qty = (int)$_POST['qty'];
$price = addslashes($_POST['price']);
$db->query("INSERT INTO products SET
invoice_nr = 2014002,
description = '".$description."',
qty = '".$qty."',
price = '".$price."'
");
}
当然,我知道网站上的注入,但这只是我创建的基本测试页面,因为我不想张贴太多的代码供您阅读,否则。
我希望这是很容易理解 - 有一个愉快的一天偷窥:)
难道你不能只是每次提交清除输入字段? –
我希望它只是一种形式,一个提交按钮:) –
我认为问题是通过'POST'只能发送一个带有特定名字的值。我的意思是,由于您添加了越来越多的输入字段,例如'name =“qty”',以前的字段值不会被发送,因为它们被最后一个字段中该名称的值覆盖。 –