如何使用带foreach循环的下拉菜单在数据库中插入数据

时间:2019-02-24 14:12:42

标签: php html

在这里,我正在创建将数据插入数据库的出勤表 单击提交按钮后。数据的插入基于2 单选按钮不存在/存在,问题出在单击提交之后 按钮,除下拉按钮外,其他组件也可以正常工作 下拉按钮仅插入一个字符串而不是服务时间 顺便说一句,我在数据库中将服务时间声明为varchar,所以我是 确信不是导致错误的数据库。也许有人 可以帮助我,非常感谢。预先谢谢你
    在此处输入代码

<?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">&times;</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>  

2 个答案:

答案 0 :(得分:1)

您的服务时间不是一个数组,因为它对所有记录都相同。因此,您需要按如下所示更改PHP代码:

$servicetime=$_POST['servicetime'];

希望它对您有帮助。

答案 1 :(得分:0)

使用$_POST['servicetime'][$id]代替$_POST['servicetime']