I can not seem to get the date to insert from my form into my data base. Please help!
here is my form
echo '
<h1>Enter New Report</h1>
<form action="submitreport.php" method="post"
enctype="multipart/form-data">
Date: <input type="text" name="Date" value='.$myDate.' /><br />
Report:<br><TEXTAREA NAME=Report ROWS=4 COLS=40></TEXTAREA><br />
<input type="submit" name="submit" value="Submit" />
</form>
';
?>
Here is what I have written to submit it to the database
$insertdate = trim($_POST['Date']);
$insertdate = mysql_real_escape_string($insertdate);
$insertdate = date('m/d/Y', $insertdate);
echo $insertdate;
mysql_query("INSERT INTO `Reports`(`Date`, `Report`) VALUES ('$insertdate','$_POST[Report]')") or die("load1 -" . mysql_error());
mysql_close();
echo "Thank You!" . " -" . "<a href='index.php' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
I'm only guessing, but your Date
field in the database is either of type date
or datetime
. The format of these are YYYY-mm-dd
. Try changing your code to:
$insertdate = date('Y-m-d', strtotime($_POST['Date']));
Also, you aren't converting the date to a timestamp before formatting it. I've added strtotime()
on the data here as well.
PHP's date
needs a timestamp as second parameter. If $insertdate
isn't one it won't work. You can though get a timestamp from a string using PHP's strtotime()
function.
Also, the value in your sql statement must be formatted according to the type used in your mysql database, for example date
, datetime
or timestamp
You can use the NOW() MySQL function to insert the current date/time into the database. You'll just need to be using the Date, DateTime or Timestamp field types for this to work.
I got it $insertdate = trim($_POST['Date']);
$insertdate = mysql_real_escape_string($insertdate);
$insertdate = strtotime($insertdate);
$insertdate = date('Y-m-d', $insertdate);
The strtotime fixed it but I had to rearrange the date format to Y-m-d
Thank you for the help