php - 为什么不在此代码中插入多个数据

标签 php mysql

我有一个 tbl_employee 表和 tbl_time 表,我想为出勤插入多个数据插入,但是当我单击提交按钮时,它只插入单个数据。但是问题出在哪里,帮我找出这个..这是插入代码

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));
            }
        }
    }
}

这是html代码

<?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>

最佳答案

这个:

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;
    }
}

如果查询成功,直接要求使用 return $massage; 中断 foreach...所以是的,您将只有一个记录。

将您的返回放在 foreach 之外。

关于php - 为什么不在此代码中插入多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40961110/

相关文章:

php - 在 Codeigniter 中获取 JSON 响应

java - 如何从Zip文件中提取Json数据并将提取的Json文件发送到前端?

mysql - manage.py inspectdb 和枚举

php - 避免重复的 mysql 连接

php - 从个人网站发送 Facebook 消息 - 可能吗?

javascript - 初学者 : Extension of file

mysql - 如果mysql中的表已经分片了,还需要分区吗?

Mysql查询混合日期时间和位置

php - 在 PHP 中使用带有弯曲文本的 SVG 创建 PDF - 不显示任何文本

javascript - 解析 JS 文件中的 PHP 数组,并在 PHP 解析数组中包含 JS 变量 -YII2