Input type =date

Hi everyone,
My HTML5 page contains a form with input type “date”. I try to add a row using the code annexed but I fail to have an additional row in my table. Can anyone suggest where have I gone wrong?
Thanks !

<?php //MyINITIAL.PHP
$MyHOST = 'localhost';
$MyUSER = 'xxx';
$MyPASS = 'yyy';
$MyDB = 'test';
?>


<?php //MyLOGIN.PHP
$MyCONNECTION = NEW MYSQLI($MyHOST,$MyUSER,$MyPASS,$MyDB);
IF(!$MyCONNECTION)
DIE('Gevald' .MYSQLI_CONNECT_ERROR());
MYSQLI_SET_CHARSET($MyCONNECTION,'UTF8');
?>


<?php // 333.php
REQUIRE_ONCE 'MyINITIAL.php';
REQUIRE_ONCE 'MyLOGIN.php';
if(isset($_POST["MyDAT"]))
{
$yosDAT = get_post($MyCONNECTION,'MyDAT');
$MyQUERY = "INSERT INTO hourShifts(shiftdate) VALUES($yosDAT)";
$YosRESULT = $MyCONNECTION->query($MyQUERY );
if(!$YosRESULT) echo $MyCONNECTION->error ;
else echo $yosDAT;
}	
echo <<<_END
<FORM action="333.php" method = "POST">
Shift Date  <INPUT TYPE = "date" name = "MyDAT"> 
			<input type = "submit" value = "ADD SHIFT">
</FORM>
_END;

function get_post($MyCONN, $var)
{
return $MyCONN->real_escape_string($_POST[$var]);
}
?>

In the table the date format is: yyyy-mm-dd. In the form it is: dd-mm-yyyy

What type is the shiftdate column in your table?



date

Sorry, I didn’t notice that comment in your original post. Clearly, as you’ve defined the column in the database a a “date”, you know that it needs to be in yyyy-mm-dd format, so if your users type it in “backwards” as dd-mm-yyyy, you’ll either have to make them not do that, or change your code so that it reverses it.

When you try to store something in a date column, MySQL will only store the data if it is a valid date. When the user types it in backwards (compared to how MySQL stores a date) it comes back as invalid and a blank date is stored. In your PHP code, you need to validate that they’ve entered a proper date, then once you’re satisfied that they have, convert it into the format that MySQL needs, then store it.

2 Likes

Thanks a lot droopsnoot !

mySQL expects dates being saved to be initially in YYYY-MM-DD format which it then converts to its own internal format. When reading it back you can tell mySQL to use whatever format you like. The YYYY-MM-DD only applies to dates that are to be converted to the internal storage format.

Thanks !

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.