简体   繁体   English

PHP日期仅不会插入Mysql

[英]PHP Date only won't insert into Mysql

I have a HTML form that has ~30 variables being inserted into mysql via php. 我有一个HTML表单,其中有〜30个变量通过php插入到mysql中。 All the other variables are inserting correctly except for the date. 除日期外,所有其他变量均正确插入。 I have looked at many of the other questions posted here and elsewhere and have still come up empty. 我已经看过这里和其他地方发布的许多其他问题,但仍然空白。

Originally I just had 3 inputs on the HTML form (year, month, day) setup as so: 最初,我在HTML表单(年,月,日)设置上只有3个输入,如下所示:

     <span class="form-sub-label-container" style="vertical-align: top">
        <input class="form-textbox" id="year_6" name="lengthOf[0]" type="date" value="" />
        <span class="date-separate"> &nbsp;- </span>
        <label class="form-sub-label" for="year_6" id="sublabel_year" style="min-height: 13px;"> Start Date </label>
      </span>

     <span class="form-sub-label-container" style="vertical-align: top">
        <input class="form-textbox" id="month_6" name="lengthOf[1]" type="date" size="2" maxlength="2" value="" />
        <span class="date-separate"> &nbsp;- </span>
        <label class="form-sub-label" for="month_6" id="sublabel_month" style="min-height: 13px;"> Month </label>
      </span>

     <span class="form-sub-label-container" style="vertical-align: top">
        <input class="form-textbox" id="day_6" name="lengthOf[2]" type="date" size="2" maxlength="2" value="" />
        <label class="form-sub-label" for="day_6" id="sublabel_day" style="min-height: 13px;"> Day </label>
      </span>

With the php for this portion being: 随着这部分的PHP是:

$LengthOfService = $_POST['lengthOf'][0]."-".$_POST['lengthOf'][1]."-".$_POST['lengthOf'][2];

A print_r($LengthOfService) would show a correctly formatted date yyyy-mm-dd, however in the column that holds the date (it is DATE type) it shows 0000-00-00. print_r($LengthOfService)将显示正确格式的日期yyyy-mm-dd,但是在保存日期(它是DATE类型)的列中,它显示了0000-00-00。

SOOOOO, I then decided to try and change the input type on the HTML form to date so there would only be 1 POST variable to load instead of 3: SOOOOO,然后我决定尝试将HTML表单上的输入类型更改为最新版本,以便仅加载1个POST变量,而不是3个变量:

          <span class="form-sub-label-container" style="vertical-align: top">
            <input class="form-textbox" id="year_6" name="lengthOf" type="date" value="01/01/2000" />
            <label class="form-sub-label" for="year_6" id="sublabel_year" style="min-height: 13px;"> Start Date </label>
          </span>

so now the php was simply: 所以现在的php就是:

$LengthOfService = $_POST['lengthOf'];

That was still a no-fire, so kept kept on with the reading and decided to go a head and try to format it even though it seemed to be already formatted correctly. 那仍然是不可行的,因此请继续阅读并决定努力尝试格式化它,即使它似乎已经正确格式化了。

$LenthOfService = date("Y-m-d", strtotime($_POST['lengthOf']));

Still showing up in the mysql column as 0000-00-00, all the while print_r($LengthOfService) still produced the correct format... 仍然在MySQL列中显示为0000-00-00,而print_r($LengthOfService)产生正确的格式...

Just for reference this is how I'm inserting into mysql: 仅供参考,这是我插入mysql的方式:

$insert = "INSERT INTO table (ColumnNames) VALUES ('$Values')";

mysql_query($insert)
   or die (mysql_error());

Hopefully I've provided enough information for someone to either be able to tell what I'm doing wrong or any other steps I can try. 希望我已经为某人提供了足够的信息,以便能够告诉我我做错了什么或我可以尝试的任何其他步骤。 I'm not getting any errors either. 我也没有任何错误。

I tested with: 我测试了:

$LenthOfService = date("Y-m-d", strtotime($_POST['lengthOf']));
$insert = "INSERT INTO table (ColumnNames) VALUES ('$LenthOfService')";
echo $insert;

and I got: 我得到:

INSERT INTO table (ColumnNames) VALUES ('2016-01-06') INSERT INTO表(ColumnNames)值('2016-01-06')

I suggest that you check and make sure you are not passing NULL, for debugging, print the query like I just did above. 我建议您检查并确保未传递NULL,以便进行调试,像上面一样打印查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM