dsapkqaduj6718493 2016-12-04 16:55 采纳率: 100%
浏览 9
已采纳

为什么多个数据不会插入此代码中

I have a tbl_employee table and tbl_time table, I want to insert multiple data insert for attendance but when I click submit button it's insert only single data . but where is the problem ,help me to find out this..this is insert code

require './db_connect.php';
class Time extends Db_connect {

    protected $link;

    public function __construct() {
        $this->link = $this->database_connection();
    }

    public function attendance_insert($data) {
        extract($data);
        $cur_date = date('Y-m-d');

        foreach ($time_attendance as $attn_key => $attn_value) {
            if ($attn_value == 'P') {
                $SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance)VALUES('$attn_key','$cur_date','P')";
                $atten_date=mysqli_query($this->link, $SQL);
            } else if ($attn_value == 'A') {
                $SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance)VALUES('$attn_key','$cur_date','A')";
                $atten_date=mysqli_query($this->link, $SQL);

            }
            if ($atten_date) {
                $massage = "<div class='alert alert-success text-center'><h5>Attendance insert successfully</h5></div>";
                return $massage;
            } else {
                die('Attendance insert query problem' . mysqli_error($this->link));
            }
        }
    }
}

this is html code

<?php

    require_once './time.php';
    $obj_time = new Time()
    $massage = '';
    if (isset($_POST['btn'])) {
        $massage = $obj_time->attendance_insert($_POST);
    }

    $employee_view = $obj_employee->employee_all_view();
    ?>
    <div class="container-fluid">
        <div class="row">
            <center>
                <a href="time.php" class="btn btn-sm btn-default glyphicon glyphicon-backward pull-left" id="show_form"></a>
                <span style="font-size:1.8em;">Attendance form</span>
            </center>
        </div>
    </div>
    <hr/>


    <?php echo $massage; ?>
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">

                <div class="panel panel-body panel-default">
                    <div class="well text-center" style="font-size:15px;">
                        <strong>Date :</strong>
                        <?php $current_date = date('Y-M-d');
                        echo $current_date; ?>
                    </div>
                    <form  class="form-horizontal" method="post">
                        <table class="table table-striped table-responsive text-center">
                            <tr>
                                <td><b>Serial</b></td>
                                <td><b>Name</b></td>
                                <td><b>ID</b></td>
                                <td><b>Attendance</b></td>
                            </tr>
                            <?php
                            $i = 0;
                            while ($employee_info = mysqli_fetch_assoc($employee_view)) {
                                extract($employee_info);
                                $i++;
                                ?>
                                <tr>
                                    <td><?php echo $i; ?></td>
                                    <td><?php echo $employee_first_name . ' ' . $employee_last_name; ?></td>
                                    <td><?php echo $employee_id; ?></td>
                                    <td>
                                        <input type="radio" name="time_attendance[<?php echo $employee_id; ?>]" value="P">P &nbsp;
                                        <input type="radio" name="time_attendance[<?php echo $employee_id; ?>]" value="A">A
                                    </td>
                                </tr>
                            <?php } ?>

                            <tr>
                                <td colspan="4">
                                    <input type="submit" class="btn btn-primary btn-block" name="btn" value="submit"/>
                                </td>
                            </tr>

                        </table>
                    </form>
                </div>
            </div>
        </div>
    </div>
  • 写回答

1条回答 默认 最新

  • drag2458 2016-12-04 21:00
    关注

    This :

    foreach($time_attendance as $attn_key => $attn_value){
        $SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance) 
            VALUES('$attn_key','$cur_date','A')";
        $atten_date = mysqli_query($this->link, $SQL);
    
        if ($atten_date) {
            $massage = "<div class=''><h5>Attendance insert successfully</h5></div>";
            return $massage;
        }
    }
    

    Literally ask to break the foreach with return $massage; if you query succeed... So yes, you'll only have one record.

    Put your return outside the foreach.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?