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 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?