加速 INSERT 语句

Speed up INSERT statement

我创建了一个 php 应用程序,它可以为给定的员工创建全年的记录。 我的应用程序接受以下输入: 我使用了以下代码。

<?php
$name=$_SESSION['name'];
$timeofdeparture="00-00-00";          
$timeofattendance="00-00-00";
$absent="Absent";
$startdate = '2015-01-01';              
$end_date ='2016-01-01';                
include 'config.php'; 
while (strtotime($startdate) <= strtotime($end_date))
{
    $flag=2;
    $sql2="INSERT INTO attendance(emp, dateofattendance, status,flag,timeofattendance,timeofdeparture)VALUES('$name', '$startdate', '$absent','$flag','$timeofattendance','$timeofdeparture') ";
    $result2 = $conn->query($sql2);
    $startdate = date ("Y-m-d", strtotime("+1 day", strtotime($startdate)));
}  
?>

但这让我的应用变慢了,因为插入一年的所有记录需要很长时间。有什么有效的方法来优化这个操作吗?我想在这里做的就是当管理员点击 "add user" 按钮时,他的一年记录必须是 created.After 一年我可以再次使用这个脚本。或者我们可以优化此查询以插入长达 3 年的记录吗? 提前致谢

像这样使用

        $sql2 =" INSERT INTO attendance(emp, dateofattendance, status,flag,timeofattendance,timeofdeparture)VALUES ";
            $sql_values = '';
            while (strtotime($startdate) <= strtotime($end_date))
            {
                $flag=2;
               $sql_values .=" ('$name', '$startdate', '$absent','$flag','$timeofattendance','$timeofdeparture') ,";

                $startdate = date ("Y-m-d", strtotime("+1 day", strtotime($startdate)));
            }  
$sql2 = $sql2.$sql_values;
    if($sql_values){
             $result2 = $conn->query($sql2);
        }