[英]PHP MySQL Insert into database
早上!
我正在嘗試創建一個登錄頁面,以便我們的員工可以將產品添加到我們的發票中。 我已經為您創建了一個測試頁,以便您可以輕松地遵循,甚至可以看到此代碼的想法。 我們的員工現在可以在頁面上僅將產品添加到一張發票(因為它是測試頁),發票為invoice_nr2014002。我希望能夠以一種形式添加多個產品。我使用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."'
");
}
當然,我知道該站點上的注入,但這只是我創建的基本測試頁,因為我不想發布太多代碼供您閱讀。
我希望這很容易理解-祝你有美好的一天:)
您可以使用不同的名稱生成表單,例如:
<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");
對於所有輸入,輸入名稱,例如
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.