将 html 日期选择器设置为 null [英] Set html datepicker to null

查看:82
本文介绍了将 html 日期选择器设置为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 php 页面上有一个表单,用于提交有关事件的信息.提交的信息存储在 MySQL 数据库中.

如果用户未从日期选择器中选择日期,则会将 0000-00-00 插入到数据库中.该列设置为默认为空,让我认为这种行为是由日期选择器引起的.

谁能建议一种允许插入空值的方法?

注意 - 我使用的是默认的 html 日期选择器,而不是 jqueryUI 版本.

编辑 - 仔细检查是因为数据库中的数据类型是日期,与日期选择器无关.

表格

<div class="textWrap"><label for="dateEndInput" id="title3">结束日期</label><input id="dateEndInput" name="dateEndInput" type="text" value="" tabindex="3">

<input name="submit" type="submit" value="Submit"/></表单>

提前致谢

解决方案

在执行 SQL 事务之前添加一个额外的 PHP 语句如何,该语句专门检查该值和 var 类型,然后在匹配这些条件时将其设置为 null?

if ($date && $date === '0000-00-00') {$日期=空;}

或者,有一个内置的 date_parse 函数

$date = '0000-00-00';$parse_date = date_parse($date);如果 ($parse_date['error_count'] > 0) {$日期=空;}

但是,还有一个数据库端解决方案,您可以将列更改为不接受 0000-00-00 作为日期.参见 http://dev.mysql.com/doc/refman/5.1/en/sql-mode.html#sqlmode_no_zero_date

I have a form on a php page that submits information about events. Information submitted is stored in a MySQL database.

When a user does not select a date from the date picker, 0000-00-00 is inserted into the database. The column is set to default to null, leading me to think that this behavior is caused by the datepicker.

Can anyone suggest a way to allow insertion of a null value?

NOTE - I'm using the default html datepicker, not the jqueryUI version.

EDIT - on closer inspection its because the datatype in the database is a date, nothing to do with the datepicker.

form

<form action="add_event.php" method="post">

    <div class="textWrap">
        <label for="dateEndInput" id="title3">End Date</label>
        <input id="dateEndInput" name="dateEndInput" type="text" value="" tabindex="3">
    </div>

        <input name="submit" type="submit" value="Submit" />

</form>

Thanks in advance

解决方案

How about having an additional PHP statement before you do your SQL transaction that checks specifically for that value and var type and then setting it to null if it matches those conditions?

if ($date && $date === '0000-00-00') {
    $date = null;
}

Alternatively, there's an inbuilt date_parse function

$date = '0000-00-00';
$parse_date = date_parse($date);

if ($parse_date['error_count'] > 0) {
    $date = null;
}

However, there's also a DB side solution where you can change the column to not accept 0000-00-00 as a date. See http://dev.mysql.com/doc/refman/5.1/en/sql-mode.html#sqlmode_no_zero_date

这篇关于将 html 日期选择器设置为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆