4

I have this database (mysql):

enter image description here

And I have code:

$day = $app->request->post('day');
            $startLocation = $app->request->post('startLocation');
            $datum = new DateTime();
            $startTime = $datum->getTimestamp(); //this line is IMPORTANT
            global $user_id;
            $db = new DbHandler();

            // creating new task
            $day_id = $db->createDay($user_id, $day, $startLocation, $startTime); ETC...

but I cant submit that timestamp data into database in startTime witch have type timestamp

Why? How to do that?

LaraBeginer
  • 169
  • 1
  • 3
  • 15
  • Do you get any error? If yes, can you please provide the error text? – ProgramFOX Dec 06 '14 at 15:07
  • no,becouse backend is REST api service on which I try to add data ... I send this to database: startTime: 1417878370, but that data not been sbmited on database – LaraBeginer Dec 06 '14 at 15:08

1 Answers1

6

If your column data type is TIMESTAMP, then it should be:

$datum = new DateTime();
$startTime = $datum->format('Y-m-d H:i:s');

Note: ->getTimestamp() returns unix timestamp

And modify your wrapper function:

public function createDay($user_id, $day, $startLocation, $startTime) { 
    $stmt = $this->conn->prepare("INSERT INTO days(day,startLocation,startTime) VALUES(?,?,?)"); 
    $stmt->bind_param('sss', $day, $startLocation, $startTime); // change the d into s in your types
    $result = $stmt->execute(); 
    $stmt->close();
}
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • hm, again dont work, I send this data: startTime: "2014-12-06 16:10:05" , but that data is not in database – LaraBeginer Dec 06 '14 at 15:10
  • @LaraBeginer make sure that all fields are provided, you have 8 fields in total – Kevin Dec 06 '14 at 15:13
  • maybe is my function problem: public function createDay($user_id, $day, $startLocation, $startTime) { $stmt = $this->conn->prepare("INSERT INTO days(day,startLocation,startTime) VALUES(?,?,?)"); $stmt->bind_param("ssd", $day, $startLocation, $startTime); $result = $stmt->execute(); $stmt->close(); – LaraBeginer Dec 06 '14 at 15:15
  • thanks, that right but with bind_param "sss" works fine ... thans again – LaraBeginer Dec 06 '14 at 15:16
  • @LaraBeginer yes, that was i was going to point out, you're inputting a string now, not a digit. im glad this helped – Kevin Dec 06 '14 at 15:18