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 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加