我有一个带有标准文本字段的表单。我在 http://phpmysqlmania.blogspot.com/p/dynamic-table-row-inserter.html 的帮助下创建了一个重复的表。我已经将它发布到 Mysql 了。但问题是它只发布了 1 行。加法表无处可去。
我简化了代码(很长) 这是我的代码:
JS
<script type='text/javascript'>
$(document).ready(function() {
var currentItem = 1;
$('#addnew').click(function(){
currentItem++;
$('#items').val(currentItem);
var strToAdd = '<tr><th colspan="100%"></th></tr><tr><tr><th>Type</th><th>Description</th><tr><td align="center"><select name="typeofunit'+currentItem+'" id="typeofunit'+currentItem+'" ><option value="DX">DX</option><option value="Chiller">Chiller</option></select></td><td align="center"><input name="unitdescription'+currentItem+'" id="unitdescription'+currentItem+'"></td></tr>';
$('#data').append(strToAdd);
});
});
index.php
<form action="function.php" method="post">
<table>
<tr><th>Company Name</th><th>Street</th><th>City</th><th>State</th><th>Zip</th><th>Country</th> </th></tr>
<td><input type="text" id="company" name="company" ></td>
<td><input id="street" name="street" ></td>
</table>
<br>
<table>
<table>
<tr><th>DX or Chiller</th><th>Equipment Description</th></tr>
<tr>
<td align="center">
<select name="typeofunit1” id="typeofunit1” >
<option value="DX">DX</option>
<option value="Chiller">Chiller</option>
</select>
</td>
<td align="center">
<input name="unitdescription1” id="unitdescription1”>
</td>
</tr>
</table>
<input type="button" id="addnew" name="addnew" value="Add Another Unit" />
<input type="submit" value="Submit" />
</body>
</html>
函数.php
<?php
$con = mysql_connect("localhost","ptgpfaso_root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydatabase", $con);
for( $i = 0 ; $i <= $count; $i++ )
{
$company = $_POST['company'];
$street = $_POST['street'];
$typeofunit = $_POST['typeofunit' . ($i+1)];
$unitdescription = $_POST['unitdescription' . ($i+1)];
$sql = "INSERT INTO equips (company,street,typeofunit,unitdescription,)
VALUES
('".$company."',
'".$street."',
'".$typeofunit."',
'".$unitdescription."',
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo " Submitted to MySQL ";
}
?>
最佳答案
您在插入查询中添加了额外的 ,
,例如 unitdescription,
和 '".$unitdescription."',
并且未正确关闭 )
也是查询。
$sql = "INSERT INTO equips (company,street,typeofunit,unitdescription)
VALUES
('".$company."',
'".$street."',
'".$typeofunit."',
'".$unitdescription."');
而不是
$sql = "INSERT INTO equips (company,street,typeofunit,unitdescription,)
VALUES
('".$company."',
'".$street."',
'".$typeofunit."',
'".$unitdescription."',
另外,
for( $i = 0 ; $i < $count; $i++ )
------------------^
而不是
for( $i = 0 ; $i <= $count; $i++ )
注意:使用 mysqli_* 函数或 PDO 而不是使用 mysql_* 函数(已弃用)
关于php - Mysql 表行添加不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21239748/