update/insert query failing to save to database in J1.5

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
ewel
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Mon Oct 01, 2007 11:35 am

update/insert query failing to save to database in J1.5

Post by ewel » Wed Jan 16, 2008 11:48 am

I am trying to convert a module to Joomla 1.5 and running into a bit of trouble. I can see that most of my code works, but when it comes to saving to the database it fails.

The relevant bit of code in issue is the following - in which $dbtext is what already exists in the database (works), if anything, $newtext is obtained from JRequest::getVar('newtext') (works), and $usrid is taken from $user = &JFactory::getUser(); $usrid = $user->get('id'); (also works).

Code: Select all

if($dbtext) {
   $query = "UPDATE #__mytext SET text = '".$newtext."' WHERE id = ".$usrid;
   $db->setQuery($query);
   $db->query();
} else {
   $query = "INSERT INTO #__mytext (id,text) VALUES ('".$usrid."','".$newtext."')";
   $db->setQuery($query);
   $db->query();
}


Looking around I found information about JTable but I have to admit that I just do not understand what to do with it.

Would anyone be able to tell me if I made some mistake or how I should do this differently? Please do tell me if the above code is not enough to help.


UPDATE: Solved it after reading another post somewhere. Turns out that the code to create a table missed $db->query(); after $db->setQuery($query);.
I'd still like to find a good starters resource on working with the database in J1.5. I have seen that there is lots of information but I think I am missing the first few steps on the ladder.
Last edited by ewel on Thu Jan 17, 2008 7:51 pm, edited 1 time in total.

Post Reply