2014-02-13 281 views
0

早上好!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."' 
"); 
} 

当然,我知道网站上的注入,但这只是我创建的基本测试页面,因为我不想张贴太多的代码供您阅读,否则。

我希望这是很容易理解 - 有一个愉快的一天偷窥:)

+0

难道你不能只是每次提交清除输入字段? –

+0

我希望它只是一种形式,一个提交按钮:) –

+0

我认为问题是通过'POST'只能发送一个带有特定名字的值。我的意思是,由于您添加了越来越多的输入字段,例如'name =“qty”',以前的字段值不会被发送,因为它们被最后一个字段中该名称的值覆盖。 –

回答

0

所有输入给像

name="description[]" 

注意[]的名字,这样我们就可以访问PHP的所有值。

而在PHP得到像

addslashes($_POST['description'][0]); //for the first product description 
addslashes($_POST['description'][1]); //for the second product description if any 
addslashes($_POST['description'][2]); //for the third product description if any 
+0

不是'$ description = addslashes($ _ POST ['description'] [0]);''和'$ description = addslashes($ _ POST ['description'] [0]);'相同? –

+0

我的错误,请参阅已编辑的答案 – 2014-02-13 14:31:58

+0

那么,我必须在查询中添加每个数组的编号吗? –

1

的发布说明和其他的东西在循环您可以生成具有不同名称的形式,例如:

<script type="text/javascript"> 
var i = 1; 

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'+i+'" value=""></td>'; 
cell2.innerHTML = '<td>Qty <input type="text" name="qty'+i+'" value=""></td>'; 
cell3.innerHTML = '<td>Price <input type="text" name="price'+i+'" value=""></td>'; 
cell4.innerHTML = '<td><input type="button" onclick="removeRow(this)" value="Remove"></td>'; 

i++; 
} 

removeRow = function(el) { 
    $(el).parents("tr").remove()  
} 
</script> 

然后在PHP中可以只是遍历所有名称:

$i=1; $values = ''; 
while(isset($_POST['description'.$i])) { 
$i++; 
$values .= "($_POST[description$i],$_POST[qty$i],$_POST[price$i])"; // add escaping 
    } 

mysql_query("insert into table_name (column1,column2,column3,...) 
VALUES $values"); 
+0

感谢bro,我会试试这个家:) –

+0

如果我不想插入想要更新当前行在一个页面中,我应该怎么做呢? –

+0

添加到查询'ON DUPLICATE KEY UPDATE column1 = VALUES(column1),column2 = VALUES(column2);'see [link](http://stackoverflow.com/questions/3432/multiple-updates-in-mysql) – Kudlas