doujiao7520 2013-11-12 11:26
浏览 49
已采纳

PHP PDO中的DATE_ADD sql

I am trying to insert a new row in my database with a date and the same date plus 1 week.

I use PDO in php, don't know if it is relevant.

This is the code which fails:

$stmtEncInsert = $db->prepare("INSERT INTO test
                                        (id_Event,   NIF,  name , description,  image,  start_date, end_date) 
VALUES (:idOfEvent, :NIF, :name, :description, :image, :startDate, DATE_ADD(:date_end,INTERVAL 1 WEEK)
                                        )");

        $stmtEncInsert->execute(array(':idOfEvent' => $idOfEvent, ':NIF'=> $NIF, ':name' => $resEnc['name'], ':description' => $resEnc['description'],
                                      ':image' => $resEnc['image'], ':startDate' => $date_end, ':date_end' => $date_end));

I get some data from other queries (example: $resEnc['..']) but all data is ok. I tried to print in a file all data and all of them are ok, they exist.

The problem is that the last field (end_date) is always giving error, always says: you cannot leave this field empty...

I think it is problem from DATE_ADD or so, but I haven't found anything related to this.

I tested deleting DATE_ADD and just using the current_date and it works so I guess this is the problem.

Any idea?

I use MySQL as engine

This is the output of the exception given:

 Integrity constraint violation: 1048 Column 'end_date' cannot be null

$date_end contains 0000-00-00 it is a valid date and field is DATE type got from DB from another query

  • 写回答

1条回答 默认 最新

  • drcx71276 2013-11-12 11:41
    关注

    0000-00-00 is not a valid date. MySQL can be configured to store invalid or incomplete dates, but that doesn't make them valid. As such, attempting to add one week will not return a valid date but NULL:

    mysql> SELECT DATE_ADD('0000-00-00', INTERVAL 1 WEEK);
    +-----------------------------------------+
    | DATE_ADD('0000-00-00', INTERVAL 1 WEEK) |
    +-----------------------------------------+
    | NULL                                    |
    +-----------------------------------------+
    1 row in set, 1 warning (0.03 sec)
    

    To sum up: as the error message explains, if you've designed end_date to be mandatory, it cannot be empty. Either assign it a proper value or allow it to be NULL.


    Edit #1: If you want to force that '0000-00-00' plus 1 week equals '0000-00-00' you can do this:

    COALESCE(DATE_ADD('0000-00-00', INTERVAL 1 WEEK), '0000-00-00')
    

    IMHO, dealing with '0000-00-00' dates does not provide any benefit and makes everything more convoluted but it's your code anyway ;-)

    Edit #2: Users don't type zeroes in field dates. Your PHP code needs to detect empty dates and insert NULL:

    if( is_valid_date($_POST['start_date']) ){
        $start_date = $_POST['start_date'];
    }else{
        $start_date = NULL;
    }
    $stmtEncInsert = $db->prepare("INSERT INTO test (start_date) VALUES (:start_date)");
    $stmtEncInsert->execute(
        array('test_date' => $start_date),
    );
    

    But you have a database design problem: the end_date column is mandatory, yet users are not required to enter it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含