5

This sounds silly but for the life of me I cannot figure it out. I have tried everything.

The problem is that I cannot figure out how to get my date string into my MySQL table... I've been converting to Unix first to account for variations in input format..

$_POST['date'] is in this format:19-1-2013 4:43:32

$datestr= strtotime($_POST['date']);
    echo $datestr;
    $sql = "INSERT INTO `calendar` (`date`, `title`, `event`) VALUES ('FROM_UNIXTIME(".$datestr.")', '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['desc'])."')";
    $query = mysql_query($sql);

This is the most recent thing I've tried and I'm still getting all zeros in my DATETIME field.

Please, what am I doing wrong?

John Woo
  • 258,903
  • 69
  • 498
  • 492
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • possible duplicate of [How to convert timestamp to datetime in MySQL?](http://stackoverflow.com/questions/5362874/how-to-convert-timestamp-to-datetime-in-mysql) – trejder Dec 10 '14 at 13:49

1 Answers1

12

remove single quotes aroung the function FROM_UNIXTIME because it will make it a value. eg

$sql = "INSERT INTO `calendar` (`date`, `title`, `event`) VALUES (FROM_UNIXTIME(".$datestr."), '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['desc'])."')";

As a sidenote, your query is vulnerable with SQL Injection, please see the article below to learn how to protect from it

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492