我有什麼似乎是一個相對簡單的問題,導致比我想象的更多的問題。正如您可能知道的那樣,HTML5不再支持表單字段的「datetime」輸入,並且只支持「datetime-local」。當我嘗試通過我的網站上的表單將「datetime-local」插入到我的數據庫中時,由於「datetime-local」中包含額外字符,我顯然遇到錯誤。我正在嘗試做什麼,以及爲什麼我需要一個日期時間字段而不是他們各自字段中的日期和/或時間,是因爲我想使用碳來以不同的格式顯示我的日期時間。我怎樣才能通過HTML表單插入日期時間到MySQL表,而無需手動將值插入到我的數據庫?如何使用HTML表單將datetime正確插入MYSQL數據庫?
編輯: 這是所有我試圖實現這一目標的相關代碼。我使用Laravel來構建這個應用程序
遊戲/創建表單:
<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 = Game::create(request(['gdatetime', 'opponent', 'location', 'field', 'season_id']));
而且,在我的Game
模型,我已經定義了這一點:
protected $dates = ['gdatetime'];
這是用戶輸入的時間/日期還是記錄保存? – GrumpyCrouton
這是一個用戶輸入日期時間@GrumpyCrouton – hoolakoola