I'm trying to insert dates using the STR_TO_DATE() function. It keeps failing for some reason. Can anyone help? Thanks
HTML:
<form action="config.php" method="POST">
Total Sales : <br><input type="text" id="total" name="total"><br>
Company Name : <br><input type="text" id="company" name="company"><br>
Sales Type : <br><input type="text" id="type" name="type"><br>
Sales Date : <br><input type="date" id="saledate" name="saledate"><br>
Date Entered : <br><input type="date" id="dateenter" name="dateenter"><br><br>
<input type="submit" value="insert">
</form>
PHP:
<?php
$con=mysqli_connect("***","***","***","***");
$Total = $_POST['total'];
$Company = $_POST['company'];
$Type = $_POST['type'];
$Saledate = $_POST['saledate'];
$Dateentered = $_POST['dateenter'];
$sql = "INSERT INTO htgsales (SaleAmount,Company,SalesType,SalesDate,DateEntered) VALUES ('$Total','$Company','$Type',STR_TO_DATE('$Saledate', '%m/%d/%Y'),STR_TO_DATE('$Dateentered', '%m/%d/%Y'))";
if(!mysqli_query($con,$sql))
{
echo 'Insert Failed';
}
else
{
echo 'Success';
}
?>
From MySQL documentation:
This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts. If the date, time, or datetime value extracted from str is illegal, STR_TO_DATE() returns NULL and produces a warning.
What's the value of the post parameters you get for the dates? Do they match the format you're passing to the function? Try doing a var_dump of $_POST and paste the values, so we can check if there's something wrong.