Mark
Mark

Reputation: 402

update record in database using jdatabase

How do to use jdatabase to update a record in Joomla3. Here is what i have so far.

$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->update('#__test AS h');
$query->set('h.name = 'apple', h.description= 'orange', h.url = 'bannana'');
$query->where('h.id=1');    
$db->setQuery($query);

Am i missing something simple?

Upvotes: 3

Views: 2394

Answers (1)

Joseph Leedy
Joseph Leedy

Reputation: 4225

I just spent the day banging my head against the wall with this too. You're very close, but you just need some minor tweaks.

$query->set('h.name = 'apple', h.description= 'orange', h.url = 'bannana'');

should be (note the quotes):

$query->set('h.name = "apple", h.description= "orange", h.url = "bannana"');

Also:

$db =& JFactory::getDBO();

will throw a "Strict Standards" warning in developer mode. Just remove the ampersand.

The missing piece:

try {
    $result = $db->execute();
} catch (Exception $e) {
    die($e->getMessage());
}

P.S. I realize this answer is a bit late so I hope that you've solved your problem by now. I am posting this answer for those who come across it later and can't find the solution in Joomla's shitty documentation.

Upvotes: 4

Related Questions