I can't get the created
column filled when inserting items into the database. I am using this code:
function createRecordInLSFCDBTable($id, $userId) {
echo 'createRecordInLSFCDBTable: ' . $id . ', ' . $userId . '<br>';
// Collect current date and time
// Try 1
// $date = date("Y-m-d h:i:s");
// $date = strtotime($date);
// Try 2
// $date = JFactory::getDate('now', new DateTimeZone('Europe/Berlin'))->format('Y-m-d h:i:s');
// $date = strtotime($date);
// Try 3
// $date = JFactory::getDate()->toSql();
var_dump($date);
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Insert columns.
$columns = array('listingid', 'user', 'created_by', 'created');
// Insert values.
$values = array($id, $userId, $userId, $date);
// Prepare the insert query.
$query
->insert($db->quoteName('#__table'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
// Set the query using our newly populated query object and execute it.
$db->setQuery($query);
$db->execute();
}
I first tried without using Joomla functions with result error 1064.
Then tried after some SO & J! forum searching try 2 and result error 1064.
Some more searching brought me by try 3 and this resulted into Call to a member function toSql() on string
.
When I remove the date from the insert columns and insert values lines then it goes through without any issues. Record is created but I need the created
column also be filled.
The full output for 1064 is 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '22:55:38)' at line 3
Invalid status line from script 'index.php': 1064
for try 3 it does not give an errorline in the log.datetime type
orcreated
. Now is hoping I don't get downvoted like crazy. Thank you for your contribution, even after an answer is already posted. That shows some true dedication, and I salute you for that. Have a great day.