0

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>";
?>
Steven
  • 61
  • 1
  • 3
  • 13
  • What format is the Date field in your database? – andrewsi Oct 04 '12 at 14:07
  • Where is the `$myDate` variable defined? – rsz Oct 04 '12 at 14:08
  • does $myDate contain timestamp? otherwise `date` function will return false or raise a WARNING message. use [`strtotime`](http://am.php.net/manual/en/function.strtotime.php) when passing parameter to `date` – haynar Oct 04 '12 at 14:10
  • What half-assed book/site taught you `mysql_query`? It's been (at least unofficially) deprecated for years now. Check out PDO and mysqli. – cHao Oct 04 '12 at 14:37
  • Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the [*red box*](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). – tereško Oct 04 '12 at 15:25

4 Answers4

3

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.

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
2

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

Zulakis
  • 7,859
  • 10
  • 42
  • 67
  • I still do not know why the default of mysql is Y-m-d but changing the format of my date and using the strtotime() function fixed the problem. – Steven Oct 04 '12 at 14:13
  • 1
    Actually, the second parameter to `date()` is optional - without one, it will give you the current date/time. *If* you specify one it needs to be a Unix-timestamp. – newfurniturey Oct 04 '12 at 14:17
0

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.

james_tookey
  • 885
  • 6
  • 12
  • I was using the date('m/d/Y') function just to put todays date in the form however I needed it to be allowed editing. – Steven Oct 04 '12 at 14:12
0

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

Steven
  • 61
  • 1
  • 3
  • 13