dpauxqt1281 2017-05-22 18:03
浏览 556
已采纳

如何使用HTML表单将日期时间正确插入MYSQL数据库?

I have what seems to be a relatively simple issue that has caused more problems than I thought it would. As you probably know, HTML5 no longer supports the "datetime" input for a form field, and only supports "datetime-local". When I try to insert "datetime-local" into my database through a form on my website, I obviously get an error because of the extra character included in "datetime-local". What I am trying to do, and why I need a datetime field as opposed to just a date and/or time in their own respective fields is because I want to use Carbon to display my datetime in different formats. How can I insert datetime through HTML form into mysql table without manually inserting the value into my database?

EDIT: Here is all of the relevant code that I am trying to achieve this with. I am using Laravel to build this application

game/create form:

<select name="season_id">

        @foreach ($seasons as $season)

        <option name="season_id" value="{{ $season->id }}">{{ $season->season }} {{ $season->year }}</option>

        @endforeach

</select>

<label for="inputDateTime" class="sr-only">DateTime</label>
    <input type="datetime-local" name="gdatetime" id="inputDateTime" class="form-control" placeholder="DateTime" required autofocus>

<label for="inputOpponent" class="sr-only">Opponent</label>
    <input type="text" name="opponent" id="inputOpponent" class="form-control" placeholder="Opponent" required autofocus>

<label for="inputLocation" class="sr-only">Location</label>
    <input type="text" name="location" id="inputLocation" class="form-control" placeholder="Location" required autofocus>

<label for="inputField" class="sr-only">Field</label>
    <input type="text" name="field" id="inputField" class="form-control" placeholder="Field" required autofocus>

game controller:

$game = Game::create(request(['gdatetime', 'opponent', 'location', 'field', 'season_id']));

Also, in my Game model, I have defined this:

protected $dates = ['gdatetime'];
  • 写回答

3条回答 默认 最新

  • doubleyou1001 2017-05-22 18:28
    关注

    You can always parse your datetime inputs to carbon instance and store them in the database. Parsing will take care of the headache that comes with formatting the input before storing. You can then display them in any format whenever needed.

    $date = \Carbon\Carbon::parse($request->input('datetime'));
    

    Edit : Based on your comments and update, do this.

    $data = request(['opponent', 'location', 'field', 'season_id']);
    
    $data['gdatetime'] = \Carbon\Carbon::parse(request('gdatetime'));
    
    $game = Game::create($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝