简体   繁体   English

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

[英]Date value recorded as 0000-00-00 in Mysql database from html form

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.` 我知道同样的问题已被问过几次,但我似乎无法绕过它。我已经完成了我找到的所有解决方案,但它不适用于我的情况。'日期'被记录为毛坯在Mysql数据库中而不是正确的date.Here是PHP代码

// 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> . <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. 我花了很多时间试图解决这个问题,但都是徒劳的,请帮忙。请注意。

If your database field's datatype is date then you can change below line of code 如果数据库字段的数据类型是date,那么您可以更改下面的代码行

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

If your database field's datatype is datetime then you can change below line of code 如果数据库字段的数据类型是datetime,那么您可以更改下面的代码行

$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']));

There is space in your key 钥匙上有空格

$Date = $_POST['Date '];

change it to 改为

$Date = $_POST['Date'];

The way you are passing parameter would lead to SQL injection . 传递参数的方式会导致SQL注入 Please read about it here . 在这里阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM