[英]how to insert data in database using dropdown with foreach loop
here i am creating a attendance sheet that insert data into the database after clicking submit button. 在这里,我正在创建一个考勤表,单击“提交”按钮后将数据插入数据库。 The insertion of the data is base on the 2 radio button which is absent/present, the problem is after clicking submit button the other component works accordingly except for the dropdown button, the dropdown button insert only a single string instead of the service time by the way i declare the service time in the database as varchar so i am confident that it's not the database that causes the error.
数据的插入基于不存在/存在的2单选按钮,问题在于单击“提交”按钮后,其他组件相应地工作,除了下拉按钮之外,下拉按钮仅插入单个字符串而不是服务时间,我在数据库中将服务时间声明为varchar的方式,因此我确信不是导致错误的数据库。 maybe someone can help me any help is much appreciated.
也许有人可以帮助我,我们将不胜感激。 thank you in advance
先感谢您
enter code here 在这里输入代码
<?php
include("Sampledb.php");
include("Sample.php");
$flag=0;
if(isset($_POST['submit']))
{
foreach($_POST['attendance_status'] as $id1=>$attendance_status)
{
$name=$_POST['name'][$id1];
$id=$_POST['id'][$id1];
$date=date("Y-m-d H:i:s");
$servicetime=$_POST['servicetime'][$id1];
$result=mysqli_query($objconn,"insert into
attendrecord(id,name,attendance_status,attendance_date,servicetime)
values('$id','$name','$attendance_status','$date','$servicetime')");
if($result)
{
$flag=1;
}
}
header("Location:sampleindex.php");
exit;
}
?>
<div class="panel panel-default">
<div class="panel panel-heading">
<form action="sampleindex.php" method="Post">
<label>Service time:</label>
<select name="servicetime">
<option>00:00</option>
<option value="7:30-9:30">7:30-9:30</option>
<option value="10:30-12:30">10:30-12:30</option>
<option value="1:30-3:30">1:30-3:30</option>
<option value="4:30-6:30">4:30-6:30</option>
</select>
<a class="btn btn-info pull-right" href="view.php">View all<a/>
</div>
</div>
<div id="alert" style="display:none;" class="alert alert-success" ">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Success!</strong>Save
</div>
<h3>
<div class="well text-center">Date:<?php echo date("Y-m-d"); ?> </div>
</h3>
<div class="panel panel-body">
<tr>
<table class="table table-striped">
</tr>
<th>ID Number</th><th>Member Name</th><th>Attendace Status</th>
<?php $result=mysqli_query($objconn,"select* from attend");
$id = 0;
$counter=0;
while($row=mysqli_fetch_array($result))
{
$id++;
?>
<tr>
<td><?php echo $row['id']; ?></td>
<input type="hidden" value="<?php echo $row['id']; ?>"name="id[]">
<td><?php echo $row['name']; ?></td>
<input type="hidden" value="<?php echo $row['name']; ?>"name="name[]">
<td>
<input type="radio" name="attendance_status[<?php echo $counter; ?>]"
value="present">Present
<input type="radio" name="attendance_status[<?php echo $counter; ?>]"
value="absent">Absent
</td>
</tr>
<?php
$counter++;
}
?>
</table>
<input type="submit" name="submit" value="submit" onclick="return mess();">
</div>
<script type="text/javascript">
function mess()
{
if($flag=1){
alert ("Record Save!");
return true;}
else
alert ("Record Not Save!");
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#submit').click(function(){
$('#alert').css('display','block');
});
});
</script>
</div>
</div>
Your service time is not an array as it keeps same for all record. 您的服务时间不是一个数组,因为它对所有记录都相同。 So you need to change your PHP code as below:
因此,您需要如下更改PHP代码:
$servicetime=$_POST['servicetime'];
Hope it helps you. 希望对您有帮助。
代替$_POST['servicetime'][$id]
使用$_POST['servicetime']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.