2014-02-13 280 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