Search code examples
phpmysqldatetextbox

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.`

// 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.


Solution

  • 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. Please read about it here.