我有一个插入多条记录的表格。
该表单具有:字段用户名(A),BTR(B),使用的设施(C),样品数(D)以及与不同样品相关的记录(1,2,3 ..)。
使用此表单,我将多个记录(ABCD1,ABCD2,ABCD3,ABCD4 ...插入数据库中。
我尝试使用for循环,但它保存了最后一个记录值。我无法做到,请帮助我。
if(isset($_POST['add']))
{
$user=$_POST['user'];
$btr=$_POST['btr'];
$facility=$_POST['facility'];
$type=$_POST['type'];
$samplelocation=$_POST['samplelocation'];
$remarks=$_POST['remarks'];
$samplecount=$_POST['samplecount'];
$sql="INSERT INTO tblfacility(user,btr,facility,type,samplelocation,remarks) VALUES(:user,:btr,:facility,:type,:samplelocation,:remarks)";
$query = $dbh->prepare($sql);
$query->bindParam(':user',$user,PDO::PARAM_STR);
$query->bindParam(':btr',$btr,PDO::PARAM_STR);
$query->bindParam(':facility',$facility,PDO::PARAM_STR);
$query->bindParam(':type',$type,PDO::PARAM_STR);
$query->bindParam(':samplelocation',$samplelocation,PDO::PARAM_STR);
$query->bindParam(':remarks',$remarks,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
if($lastInsertId)
{
$_SESSION['msg']="Sample Listed successfully";
header('location:manage-books.php');
}
else
{
$_SESSION['error']="Something went wrong. Please try again";
header('location:manage-books.php');
<label>BTR No.<span style="color:red;">*</span></label>
<input class="form-control" type="text" name="btr" required="required" autocomplete="off" />
</div>
<label>BTR No.<span style="color:red;">*</span></label>
<input class="form-control" type="text" name="btr" required="required" autocomplete="off" />
</div>
<div class="form-group">
<label>Facility Used<span style="color:red;">*</span></label>
<select class="form-control" type="text" name="facility" required="required" autocomplete="off" />
<option value="AMS">AMS 14C</option>
<option value="AMS">AMS 10Be</option>
</select>
<p class="help-block"></p>
</div>
<div class="form-group">
<label>Number Of Sample<span style="color:red;">*</span></label>
<input class="form-control" type="text" name="samplecount" onchange="samplecount()" required="required" autocomplete="off" />
</div>
<td>
<table class="table table-striped small-text" id="tb" >
<tr class="tr-header">
<h4 style="color:darkGreen;" ><b><u>SAMPLE INFORMATION</u></b></h4>
<th class= "col-md-1" align="centre">Sl.No.</th>
<th class= "col-md-2" align="centre">TYPE OF SAMPLE</th>
<th class= "col-md-2" align="centre">Sample Location</th>
<th class= "col-md-6" align="centre"> Remarks</th>
<th><a href="javascript:void(0);" style="font-size:18px;" id="addMore" title="Add More Sample"><span class="glyphicon glyphicon-plus"></span></a></th>
<tr>
<td><input type="text" name="slno" value= "<?php echo $i; ?>" class="form-control" ></td>
<td><select type="text" name="type" class="form-control">
<option value="SELECT TYPE">SELECT TYPE</option>
<option value="BONE">BONE</option>
<option value="CARBONATE">CARBONATE</option>
</select></td>
<td><input type="text" name="samplelocation" class="form-control" ></td>
<td><input type="text" name="remarks" class="form-control"></td>
<td><a href='javascript:void(0);' class='remove'><span class='glyphicon glyphicon-remove'></span></a></td>
<?php }?>
</tr>
</table>
<button type="submit" name="add" class="btn btn-info" align="middle" >ADD </button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script>
$(function(){
$('#addMore').on('click', function() {
var data = $("#tb tr:eq(1)").clone(true).appendTo("#tb");
data.find("input").val('');
});
$(document).on('click', '.remove', function() {
var trIndex = $(this).closest("tr").index();
if(trIndex>1) {
$(this).closest("tr").remove();
} else {
alert("Sorry!! Can't remove first row!");
}
});
});
</script>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
在所有输入名称中添加[]
,例如:name="user[]"
。提交后,这将为您提供输入元素的数组。
因此,在您的if(isset($_POST['add']))
中,您可以像$arr_user = $_POST['user'];
这样获得该数组,然后使用for循环来循环这些数据。
您的tr标签可能就像
<tr class="tr-header">
<h4 style="color:darkGreen;" ><b><u>SAMPLE INFORMATION</u></b></h4>
<th class= "col-md-1" align="centre">Sl.No.</th>
<th class= "col-md-2" align="centre">TYPE OF SAMPLE</th>
<th class= "col-md-2" align="centre">Sample Location</th>
<th class= "col-md-6" align="centre"> Remarks</th>
<th><a href="javascript:void(0);" style="font-size:18px;" id="addMore" title="Add More Sample"><span class="glyphicon glyphicon-plus"></span></a></th>
<tr>
<td><input type="text" name="slno[]" value= "<?php echo $i; ?>" class="form-control" ></td>
<td><select type="text" name="type[]" class="form-control">
<option value="SELECT TYPE">SELECT TYPE</option>
<option value="BONE">BONE</option>
<option value="CARBONATE">CARBONATE</option>
</select></td>
<td><input type="text" name="samplelocation[]" class="form-control" ></td>
<td><input type="text" name="remarks[]" class="form-control"></td>
<td><a href='javascript:void(0);' class='remove'><span class='glyphicon glyphicon-remove'></span></a></td>
<?php }?>
</tr>