Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

php - how to insert an empty value in mysql date type field?

How to insert a NULL or empty value in a mysql date type field (NULL = yes).

If I try to insert an empty value it inserts 0000-00-00 but I want to keep it empty or NULL.

Thanks for help.

UPDATE

Please see that I have set default value as NULL

  `payment_due_on` date DEFAULT NULL,

Ok OKies

I have got it working now

function get_mysqlDate($date, $delimiter='/') {
    if(check_empty($date)) {
        return 'NULL';
    }
    list($d, $m, $y) = explode($delimiter, $date);
    //$datetime = strtotime($date);
    return "'".date('Y-m-d', mktime(0,0,0,$m, $d, $y))."'";
}




"... SET mydate = ".get_mysqldate($_POST['mydate'])." ...."

Cheers

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If that's happening, it's because the column has been defined with NOT NULL or with a default '0000-00-00'. You need to change the column definition to allow NULLs and have no default.

Your insert should be specifying NULL, as well, not '', if that's what you mean by "empty value".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...