我对这个动态行添加和发布到mysql有很大的挫败感。变量是错误的我确定。脚本也可以正常工作。我只需要帮助发布到MYSQL。 请原谅我......我是初学者。
提前致谢!
脚本工作精细!
<script type='text/javascript'>
$(document).ready(function() {
var currentItem = 1;
$('#addnew').click(function(){
currentItem++;
$('#items').val(currentItem);
var strToAdd = `<tr><th>Company Name</th><th>Street</th></tr><td><input type="text" id="company'+currentItem+'" name="company'+currentItem+'" ></td><td><input id="street'+currentItem+'" name="street'+currentItem+'" ></td></table><br><table> <tr><th>Contact Name</th><th>Contact Title/Role</th></tr><td><input type="text" id="contact'+currentItem+'" name="contact'+currentItem+'" ></td><td><input type="text" id="title'+currentItem+'" name="title'+currentItem+'" ></td></table><tr><th>DX or Chiller</th><th>Equipment Description</th></tr><tr><select name="typeofunit'+currentItem+'" id="typeofunit'+currentItem+'" ><option value="DX">DX</option><option value="Chiller">Chiller</option></select></td><td><input name="unitdescription'+currentItem+'" id="unitdescription'+currentItem+'"></td></tr>`
$('#data').append(strToAdd);
});
});
</script>
HTML 工作精细
<html>
<body>
</head>
<body>
<form action="function.php" method="post">
<table>
<tr><th>Company Name</th><th>Street</th></tr>
<td><input type="text" id="company" name="company" ></td>
<td><input id="street" name="street" ></td>
</table>
<br>
<table>
<tr><th>Contact Name</th><th>Contact Title/Role</th></tr>
<td><input type="text" id="contact" name="contact" ></td>
<td><input type="text" id="title" name="title" ></td>
</table>
<tr><th>DX or Chiller</th><th>Equipment Description</th></tr>
<tr>
<select name="typeofunit" id="typeofunit" >
<option value="DX">DX</option>
<option value="Chiller">Chiller</option>
</select>
</td>
<td>
<input name="unitdescription" id="unitdescription">
</td>
</table>
<p class="login button">
<input type="button" id="addnew" name="addnew" value="Add Another Unit" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
PHP 这就是问题?
<?php
$con = mysql_connect("localhost","ptgpfaso_root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ptgpfaso_mydatabase", $con);
for( $i = 1; $i <= $count; $i++ )
{
$company = $_POST['company'];
$street = $_POST['street'];
$typeofunit = $_POST['typeofunit'.$i];
$unitdescription = $_POST['unitdescription'.$i];
}
$sql="INSERT INTO equips (company,street,typeofunit,unitdescription)
VALUES
('".$company."','".$street."','".$typeofunit."','".$unitdescription."',)";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Thank you for your submission";
?>
答案 0 :(得分:2)
假设您在分配的变量中获得了正确的数据,那么您的查询应该是
$sql="INSERT INTO equips (company,street,typeofunit,unitdescription)
VALUES
('".$company."','".$street."','".$typeofunit."','".$unitdescription."')";
在查询结束时,您有一个不正确的,并且您还需要使用
mysql_real_escape_string(),用于每个请求的数据,如
$company = mysql_real_escape_string($_POST['company']);
$street = mysql_real_escape_string($_POST['street']);
$typeofunit = mysql_real_escape_string($_POST['typeofunit'.$i]);
$unitdescription = mysql_real_escape_string($_POST['unitdescription'.$i]);
此外,您应该启动mysqli_ *函数或PDO,因为不推荐使用mysql_ *函数。