drvc63490 2019-04-29 05:46
浏览 176

日期值在html表单的Mysql数据库中记录为0000-00-00

I know the same question has been asked several times but I cant seem to wrap my head around it.I have done all the solutions I have found but its not working in my case.'Date'is being recorded as 0000-00-00 within the Mysql database instead of a proper date.Here is the PHP code.`

// initialize variables

$Vehicle_name = "";
$Vehicle_make = "";
$Vehicle_color="";
$Number_plate = "";
$Driver_name = "";
$Number_of_passengers = "";
$Date = "";
$Time = "";
$Security = "";


$id = 0;
$update = false;

if (isset($_POST['save'])) {

    $Vehicle_name = $_POST['Vehicle_name'];
    $Vehicle_make = $_POST['Vehicle_make'];
    $Vehicle_color = $_POST['Vehicle_color'];
    $Number_plate = $_POST['Number_plate'];
    $Driver_name = $_POST['Driver_name'];
    $Number_of_passengers = $_POST['Number_of_passengers'];
    $Date = $_POST['Date '];
    $Time = $_POST['Time'];
    $Security = $_POST['Security'];


    mysqli_query($db, "INSERT INTO vehicle (Vehicle_name,Vehicle_make,Vehicle_color,Number_plate,Driver_name,Number_of_passengers,Date,Time,Security ) VALUES ('$Vehicle_name','$Vehicle_make','$Vehicle_color','$Number_plate','$Driver_name','$Number_of_passengers','$Date','$Time','$Security ')"); 
    $_SESSION['message'] = "Car registered"; 
    header('location: welcome.php');
}`.

Here is the date textbox code also.

<div class="input-group"> <label>Date :</label> <input type="date" name="Date" value="" required="yes" > </div>.

I have spent so much time trying to figure it out but all in vain,please help.Regards.

  • 写回答

2条回答 默认 最新

  • dongzhong1891 2019-04-29 06:30
    关注

    If your database field's datatype is date then you can change below line of code

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

    If your database field's datatype is datetime then you can change below line of code

    $Date = date('Y-m-d H:i:s', strtotime($_POST['Date']));
    

    Here i can see you are separating time in another field then you can change below line of code

    $Time = date('H:i:s', strtotime($_POST['Time']));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图