我想在提交时提交所有行。目前只提交一行。 这是我发布到数据库的方式:
<?php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "pro_forms_in_form")) {
$insertSQL = sprintf("INSERT INTO ho_add_stock_proforma (supplier, inv_no, `day`, `month`, `year`, product_name, descrpt, qty, cost, hawker, wholesale, retail, total) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['supplier'], "text"),
GetSQLValueString($_POST['inv_no'], "int"),
GetSQLValueString($_POST['day'], "int"),
GetSQLValueString($_POST['month'], "int"),
GetSQLValueString($_POST['year'], "int"),
GetSQLValueString($_POST['product_name'], "text"),
GetSQLValueString($_POST['descrpt'], "text"),
GetSQLValueString($_POST['qty'], "text"),
GetSQLValueString($_POST['cost'], "text"),
GetSQLValueString($_POST['hawker'], "text"),
GetSQLValueString($_POST['wholesale'], "text"),
GetSQLValueString($_POST['retail'], "text"),
GetSQLValueString($_POST['total'], "text"));
mysql_select_db($database_universal, $universal);
$Result1 = mysql_query($insertSQL, $universal) or die(mysql_error());
}
mysql_select_db($database_universal, $universal);
$query_Creditors = "SELECT company_name FROM ho_add_creditors";
$Creditors = mysql_query($query_Creditors, $universal) or die(mysql_error());
$row_Creditors = mysql_fetch_assoc($Creditors);
$totalRows_Creditors = mysql_num_rows($Creditors);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<!-- and this part help with adding more table rows -->
<script type="text/javascript">
function addRow(ho_add_stock_proforma) {
var table = document.getElementById(ho_add_stock_proforma);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
function deleteRow(ho_add_stock_proforma) {
try {
var table = document.getElementById(ho_add_stock_proforma);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e)
{
alert(e);
}
getValues();
}
</script>
</head>
<body>
<div id="wrap">
<form action="<?php echo $editFormAction; ?>" method="POST" name="pro_forms_in_form">
<!-- and this is my html table -->
<table width="666" border="0" cellpadding="2" cellspacing="2" class="table" id="ho_add_stock_proforma" name="ho_add_stock_proforma">
<tr>
<td><input type="checkbox" name="chk" style="width:40px" /></td>
<td width="122" scope="col">Product Name<input name="product_name" type="text" class="table_fields" id="product_name" /></td>
<td width="122" scope="col">Description<input name="descrpt" type="text" class="table_fields" id="description" /></td>
<td width="62" scope="col">Qty<input name="qty" type="text" class="qty" id="qty" onkeyup="getValues()" /></td>
<td width="62" scope="col">Cost per Piece<input name="cost" type="text" class="price" id="cost" onkeyup="getValues()"/></td>
<td width="62" scope="col">hawker<input name="hawker" type="text" class="price" id="hawker" /></td>
<td width="62" scope="col">Wholesale<input name="wholesale" type="text" class="price" id="wholesale" /></td>
<td width="62" scope="col">Retail<input name="retail" type="text" class="price" id="retail" /></td>
<td width="349" scope="col">Total<input name="total" type="text" class="price" id="total" onKeyUp="getValues()" /></td>
<td width="5"> <input name="button" type="button" value="Add" onclick="addRow('ho_add_stock_proforma')" style="width:40px"/></td>
<td><input name="button" type="button" value="Delete" onclick="deleteRow('ho_add_stock_proforma')" style="width:40px"/></td>
</tr>
</table>
<input name="submit" type="submit" class="button_submit" value="Submit Form" id="submit" />
<input type="hidden" name="MM_insert" value="pro_forms_in_form" />
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
您是通过Javascript向HTML表添加行,然后使用PHP添加/更新数据库中的行,是否正确?
新的HTML表行仅存在于浏览器中(您通过Javascript在客户端创建它们):服务器上运行的PHP不知道它们是否存在,因此无法在数据库中插入/更新它们。
您需要生成HTML表格服务器端的新行并重新加载浏览器页面,否则如果您在客户端生成它们,则需要在每个POST中手动包含新行。