doukong9316 2011-03-29 11:46
浏览 152
已采纳

循环带有更新日期的SQL插入语句

What do I need to do is run insert the same thing into a database only changing the date.

i.e

INSERT INTO bs_events
(
    eventDate,
    eventTime,
    title,
    spaces,
    description,
    entryFee,
    payment_required,
    max_qty,
    allow_multiple
) 
VALUES (
    '2011-03-02 00:00:00',
    '10:00',
    'Carpet Cleaner 3000',
    '4',
    'This is rad',
    '25.99',
    '1',
    '1',
    '2'
)

So I need to run this loop for a year or month or 2 years and have it add to the database but the date needs to change each day

I've got no idea how I go about making a script to do this so any help would be appreciated

Thanks

  • 写回答

3条回答 默认 最新

  • dongzhuohan7085 2011-03-29 11:58
    关注

    Here's an example. It does it all in one query.

    $row = array(
        '2011-03-02 00:00:00',
        '10:00',
        'Carpet Cleaner 3000',
        '4',
        'This is rad',
        '25.99',
        '1',
        '1',
        '2'
    );
    
    $rows = array();
    $end = new DateTime('2012-01-01');
    for($date = new DateTime($row[0]); $date < $end; $date->modify('+1 day')) {
        $row[0] = $date->format('Y-m-d H:i:s');
        $rows[] = sprintf("('%s')", implode("','", $row));
    }
    
    $query = 'INSERT INTO bs_events
    (
        eventDate,
        eventTime,
        title,
        spaces,
        description,
        entryFee,
        payment_required,
        max_qty,
        allow_multiple
    ) VALUES '.implode(',', $rows);
    

    Note: This works because the row data is known... if you accepting user input, make sure you are checking for SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化