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>";
?>
5
  • What format is the Date field in your database? Commented Oct 4, 2012 at 14:07
  • Where is the $myDate variable defined? Commented Oct 4, 2012 at 14:08
  • does $myDate contain timestamp? otherwise date function will return false or raise a WARNING message. use strtotime when passing parameter to date Commented Oct 4, 2012 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. Commented Oct 4, 2012 at 14:37
  • Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Oct 4, 2012 at 15:25

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's the same as what you posted in your answer (except all in one line), so I should hope it works too =P
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

2 Comments

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.
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.
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.

1 Comment

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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.