gpt4 book ai didi

javascript - 动态行增加了 Mysql 问题

转载 作者:行者123 更新时间:2023-11-29 08:12:56 25 4
gpt4 key购买 nike

我对这个动态行添加和发布到 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";
?>

最佳答案

假设您在分配的变量中获得了正确的数据,您的查询应为

$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_* 函数已被弃用。

关于javascript - 动态行增加了 Mysql 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21210522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com