当我将其插入到数据库中时,我只是动态地添加和删除输入字段,只有第一个输入才被插入,我已经检查了几次,但是我的代码中没有任何错误,需要帮助哪里出错了。 请提供任何解决方案,以解决该问题并将其成功插入数据库的情况。
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = 'test';
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
?>
<?php include('connection.php');
if(isset($_POST['submit'])){
$date = $_POST['field1'];
foreach( $date as $key => $d ) {
$sql = "INSERT INTO invoice (name) VALUES ('$date[$key]')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully".mysqli_affected_rows($conn);
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>multiple fields</title>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<style>
* {
.border-radius(0) !important;
}
#field {
margin-bottom:20px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<input type="hidden" name="count" value="1" />
<div class="control-group" id="fields">
<label class="control-label" for="field1">Nice Multiple Form Fields</label>
<div class="controls" id="profs">
<form class="input-append" method="post">
<div id="field">
<input type="text" name="field1[]" class="input" id="field1" data-items="8"/>
<button id="b1" class="btn add-more" type="submit">+</button>
</div>
<button type="submit" name="submit">Submit</button>
</form>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
var next = 1;
$(".add-more").click(function(e){
e.preventDefault();
var addto = "#field" + next;
var addRemove = "#field" + (next);
next = next + 1;
var newIn = '<input autocomplete="off" class="input form-control" id="field' + next + '" name="field' + next + '" type="text">';
var newInput = $(newIn);
var removeBtn = '<button id="remove' + (next - 1) + '" class="btn btn-danger remove-me" >-</button></div><div id="field">';
var removeButton = $(removeBtn);
$(addto).after(newInput);
$(addRemove).after(removeButton);
$("#field" + next).attr('data-source',$(addto).attr('data-source'));
$("#count").val(next);
$('.remove-me').click(function(e){
e.preventDefault();
var fieldNum = this.id.charAt(this.id.length-1);
var fieldID = "#field" + fieldNum;
$(this).remove();
$(fieldID).remove();
});
});
});
</script>
</body>
</html>
答案 0 :(得分:1)
这是页面刷新问题,因此请尝试重定向同一页面。
例如:
if ($conn->query($sql) === TRUE) {
echo "New record created successfully".mysqli_affected_rows($conn);
header("location:index.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
header("location:index.php");
}
我用过
header("location:index.php");
完成手术后
答案 1 :(得分:0)
您生成的动态输入的名称不是field1 [],而是field2,field3等...
var newIn = '<input autocomplete="off" class="input form-control" id="field' + next + '" name="field' + next + '" type="text">';
在您的PHP中,您仅检索$ date = $ _POST ['field1'];这是由1个元素组成的数组。
正确的解决方案是将您的第一个输入名称从field1 []重命名为field1,并保留其他名为field2,field3的输入
添加这样的隐藏输入:
<input type="hidden" name="nb_elements" value="1" >
每次创建新输入时,请将其添加到javascript中:
$("[name=nb_elements]").val(next);
在您的PHP中执行以下操作:
for($i=$_POST['nb_elements'] ; isset($_POST['field'.$i]) ; $i++ ){
$sql = "INSERT INTO invoice (name) VALUES ('$_POST['field'.$i]')";
//...
}
请注意,您可能会进行SQL注入,请使用以下功能: http://php.net/manual/en/mysqli.real-escape-string.php
答案 2 :(得分:0)
您的代码在编码实践和代码样式方面存在多个问题,但希望您能从中学习到更多的时间。
我将只解决您提到的问题。
要在POST请求中获取html表单元素作为数组,则所有元素都应具有相同的name
属性。
在您的情况下,您的JavaScript应生成新的输入字段,结果如下所示
<input autocomplete="off" class="input form-control" id="field1" name="field1[]" type="text">
<input autocomplete="off" class="input form-control" id="field2" name="field1[]" type="text">
<input autocomplete="off" class="input form-control" id="field3" name="field1[]" type="text">
但是您当前的脚本为每个新输入元素创建了唯一的名称属性。必须按如下所示进行更新
var newIn = '<input autocomplete="off" class="input form-control" id="field' + next + '" name="field1[]" type="text">';