开发者

PHP - INSERT a variable of type Time into a column in MySQL of type Time

I keep getting this error:

"Catchable fatal error: Object of class DateTime could not be converted to string"

From this code:

<?php
   $value_startTime = new DateTime();
   $value_startTime->setTime($value_HourStart,$_POST['TextBoxStartMin'],0);

   $value_endTime = new DateTime();
   $value_endTime->setTime($value_HourEnd,$_POST['TextBoxEndMin'],0);

   $query_InsertJob="INSERT INTO job (jobDesc,timeStart,timeEnd)
   VALUE ('$_POST[TextAreaProblem]','$value_startTime','$value_endTime')";
?>

These variables can have values from 00 to 23: $value_HourStart $value_HourEnd

These variables can have va开发者_Go百科lues from 00 to 59: $_POST['TextBoxStartMin'] $_POST['TextBoxEndMin']

I am having no trouble with: $_POST[TextAreaProblem]

What is it that I am doing wrong?


You need to convert those DateTime objects into strings for use in the query.

Try something like this (PDO example because I can't abide encouraging people to use the mysql_* functions)

$stmt = $db->prepare('INSERT INTO job (jobDesc, timeState, timeEnd) VALUES (?, ?, ?)');
$stmt->execute(array(
    $_POST['TextAreaProblem'],
    $value_startTime->format('H:i:s'),
    $value_endTime->format('H:i:s')
));


$value_startTime and $value_endTime are objects. Try it with $value_startTime->getTimestamp().

$query_InsertJob="INSERT INTO job (jobDesc,timeStart,timeEnd) VALUE ('$_POST[TextAreaProblem]','".$value_startTime->getTimestamp()."','"$value_endTime->getTimestamp()."')";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜