douyu3145 2014-01-18 22:11
浏览 5

动态行添加到Mysql问题

Im having massive frustrations with this dynamic row add and post to mysql. The variables are wrong Im sure. Also the script works fine. I only need help posting to MYSQL. Please forgive me... Im a beginner.

Thanks in advance!

Script WORKS FINE!

<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 WORKS FINE

<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 THIS IS THE PROBLEM?

<?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";
?>
  • 写回答

1条回答 默认 最新

  • donglei7152 2014-01-18 22:15
    关注

    Assuming you are getting correct data in assigned variables your query should be as

    $sql="INSERT INTO equips (company,street,typeofunit,unitdescription)
    VALUES
    ('".$company."','".$street."','".$typeofunit."','".$unitdescription."')";
    

    You have a , at the end of your query, which is not correct and also you need to use

    mysql_real_escape_string() for each Requested data something like

    $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]);
    

    Also you should start mysqli_* functions or PDO, since mysql_* functions are deprecated.

    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接