php-如何将MySql DATETIME结果放入类型为datetime-local的HTML输入中
我使用< input type =“ datetime-local”>存储了日期现在我要在数据库中的< input type =“ datetime-local”>中获取日期.用于更新目的.
我在做
$query="select * from exam_schedule where subject_name='$name' AND class_id='$schedule_id' limit 1";
$result= mysqli_query($connection,$query);
while ($read_all_data = mysqli_fetch_assoc($result))
{
echo "date comming from datebase::".$date=$read_all_data['date_and_time']."<br>";
echo "duration comming from datebase::".$duration=$read_all_data['duration'];
$duration=$read_all_data['duration'];
echo "<form method='post' action='edit_schedule.php'>";
echo "<input type='hidden' name='id' value='$id'>";
echo "<tr><th><input type='datetime-local' name='date' value='$date'> </th>";
echo "<th><input type='datetime-local' name='duration' value='$duration'> </th>";
echo <input type='submit' name='update' value='update'> </form></th></tr>";
}
当我回显$date和$duration时,它显示了我的值,但是当我输入value =“ $date”时,它没有显示数据库中的日期.
解决方法:
问题在于,datetime-local输入类型期望日期采用以下格式:yyyy-mm-ddThh:mm,但您要提供的是数据库创建的默认DATETIME格式,即yyyy-mm-dd hh:mm: ss.
Demo
为了对其进行更改,您需要将SQL查询更改为此:
SELECT *, DATE_FORMAT(date_and_time, '%Y-%m-%dT%H:%i') AS custom_date
FROM exam_schedule
WHERE subject_name='$name'
AND class_id='$schedule_id'
LMIT 1
因此,现在在您的结果中,您可以使用custom_date来预填您的输入.
<input type='datetime-local' name='date' value='<?php echo $row['custom_date'] ?>' >