1

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.

user8840249
  • 53
  • 1
  • 9
  • 2
    `$Date = $_POST['Date '];` <-- there is a rouge space here, after `Date`. This could be the reason the date is coming up null/empty. – Tim Biegeleisen Apr 29 '19 at 05:48
  • check date format...mysql date format is "YYYY-mm-dd" – Ashu Apr 29 '19 at 05:50
  • Note: please don’t ever put values from the user into SQL strings. Use parameters/prepared statements. It will save you a lot of trouble. Just try giving driver’s name as John O’Malley in your current code and see what happens. – Sami Kuhmonen Apr 29 '19 at 06:24

2 Answers2

0

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']));
turivishal
  • 34,368
  • 7
  • 36
  • 59
0

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.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78