我正在尝试使用jQuery从动态表单向MySQL插入数据。为了记录,我正在使用Twitter Bootstrap。 我实际上实现了插入“行”但没有数据。我现在找到的最好的帮助是here用于插入MySQL和here到jQuery脚本。
提前感谢您抽出时间。
文件:add.php
<form action="../reglas/insert.php" method="post" class="form-horizontal">
<div class="container">
<div id="content">
<div class="row">
<a class="add_line btn btn-small" href="#" style="margin-left:5px">+</a>
</div>
<br>
</div>
</div>
</div>
</div>
<div style="margin:20px auto; width:80px;">
<input class="btn btn-success" type="submit" value="Guardar">
</div>
</form>
<script>
$("#content").delegate(".add_line", "click", function() {
var content = '<div class="row">' +
'<div class="span3"><div class="input-append"><input type="text" class="span2 text-right" placeholder="0.00" id="enganche" name="enganche" value="" /><span class="add-on">%</span></div></div>' +
'<div class="span3"><div class="input-append"><input type="text" class="span2 text-right" placeholder="0.00" id="comision" name="comision" value="" /><span class="add-on">%</span></div></div>' +
'<div class="span3"><div class="input-append"><input type="text" class="span2 text-right" placeholder="0.00" id="r1" name="r1[]" value="" /><span class="add-on">%</span></div></div>' +
'<div class="span2"><div class="input-append"><input type="text" class="span1 text-right" placeholder="0.00" id="r2" name="r2[]" value="" /><span class="add-on">%</span></div></div>'+
'<a class="del_line btn btn-small" href="#" style="margin-left:5px">-</a> ' +
'<br><br>'+
'</div>';
$("#content").append(content);
return false;
});
$("#content").delegate(".del_line", "click", function() {
$(this).parent().remove();
return false;
});
</script>
文件:insert.php
连接:
$con=mysqli_connect("$host","$username","$password","$db_name");
mysqli_autocommit($con, false);
$flag=true;
查询:
$query="
INSERT INTO $tbl_name(
the_input,
)
VALUES (
'$the_input',
)";
“for”:
for ($i = 0; $i < count($_POST['the_input']); $i++) {
$the_input = $_POST['the_input'][$i];
$result = mysqli_query($con, $query);
if (!$result) {
$flag = false;
echo "Error details: " . mysqli_error($con). ". ";
}
}
if ($flag) {
mysqli_commit($con);
echo "Done!";
}
mysqli_close($con);
?>
答案 0 :(得分:0)
Okey dokey。
首先要做的事情:
不推荐使用 $.delegate()
,因此除非您使用的版本是&lt; jQuery 1.7(很多版本之前)你应该迁移到$.on()
事件绑定器/委托者函数。这将确保您的代码是未来证明(至少在他们再次更改api之前!)
其次:
我假设您已将数据包装在表单标记中,并且值实际上已发送到接收页面,这是我将如何执行此操作:
if(isset($_POST['the_input']))
{
//create connection object;
$mysqli = new mysqli($host,$username,$password,$db_name);
$query = "INSERT INTO `the_input` VALUES (";
//construct the query. Get the the_input data and paramater string (required for stmt binding)
foreach($_POST['the_input'] as $value)
{
$query .= $value . ",";
}
//remove trailing comma
$query = substr($query, 0, strlen($query)-1);
$query .= ")";
//this is just so you can see what the loop did and test the output of the query, remove this when you're confident
echo "The constructed query string is: " . $query
if($mysqli->query($query) == false)
{
trigger_error($mysqli->error);
} else {
echo "Success! Check the DB!";
}
} else {
Echo "No POST data was recieved";
}