【问题标题】:Insert a date from boostrap datepicker into MySQL date field does not work将引导日期选择器中的日期插入 MySQL 日期字段不起作用
【发布时间】:2017-06-16 01:29:04
【问题描述】:

我尝试了很多可能的解决方案,但它对我不起作用。我有以下代码:

<div class="bootstrap-iso">
    <div class="container-fluid">
        <div class="row">
           <div class="col-md-6 col-sm-6 col-xs-12">
               <!-- datepicker code begins -->
               <div class="form-group"> <!-- Date input -->
                   <label class="control-label" for="date">Closing Date</label>
                   <input class="form-control" id="date" name="date" placeholder="YYYY-  MM-DD" type="date"/>
               </div>
           </div>
       </div>    
    </div>

然后我有以下内容:

<script>
    $(document).ready(function(){
        var date_input=$('input[name="date"]'); //our date input has the name "date"
        var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
        var options={
            format: 'yyyy-mm-dd',
            container: container,
            todayHighlight: true,
            autoclose: true,
        };
        date_input.datepicker(options);
    })
</script>

在我的课堂上,我有以下代码:

$newVacancy_stmt = $db->prepare('INSERT INTO vacancies (roleID, shortTitle, longTitle, roleDescription, roleResponsibilities, roleRequirements, createdBy, creationDate, publishDate, closeDate) VALUES (:roleID, :shortTitle, :longTitle, :roleDescription, :roleResponsibilities, :roleRequirements, :createdBy, NOW(), NOW(), ' . $closingDate . '))');

我用这个转换我的发布日期:

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

然后我调用我的函数:

$addVacancy = addNewVacancyInternal($roleID, $shortTitle, $longTitle, $roleDescription, $roleResponsibilities, $roleRequirements, $createdBy, $closingDate);

它不会将记录插入数据库。一旦我取出日期(不插入日期),它就会插入记录。请问有什么建议吗?

【问题讨论】:

  • 检查 $_POST['date'] 中的值。告诉我们价值。

标签: php mysql date datepicker bootstrap-datepicker


【解决方案1】:
$db->prepare('INSERT INTO vacancies (roleID, ...., publishDate, closeDate) 
                             VALUES (:roleID, .... NOW(), :closingDate))');

并将 $closingDate 与其余命名参数一起传递。

【讨论】:

  • $closingDate = date('Y-m-d', strtotime($_POST['date']));
  • $addVacancy = addNewVacancyInternal($roleID, $shortTitle, $longTitle, $roleDescription, $roleResponsibilities, $roleRequirements, $createdBy, $closingDate); $newVacancy_stmt = $db-&gt;prepare('INSERT INTO vacancies (roleID, shortTitle, longTitle, roleDescription, roleResponsibilities, roleRequirements, createdBy, creationDate, publishDate, closeDate) VALUES (:roleID, :shortTitle, :longTitle, :roleDescription, :roleResponsibilities, :roleRequirements, :createdBy, NOW(), NOW(), :closingDate))');
  • $newVacancy_stmt-&gt;bindParam(':closingDate', $closingDate, PDO::PARAM_STR); $addNewVacancy = $newVacancy_stmt-&gt;execute(); 还是不行,谢谢。
  • 不,只要我插入没有插入日期的记录。
  • 在插入日期时是否有错误?同时启用查询日志并检查查询到达数据库时的外观。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 2016-07-03
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
相关资源
最近更新 更多