[SOLVED] Problems with MySQL INSERT, UPDATE, and DELETE

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
jayred
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sat Aug 11, 2007 7:02 am

[SOLVED] Problems with MySQL INSERT, UPDATE, and DELETE

Post by jayred » Thu Jan 03, 2008 8:39 am

I'm working on a bunch of new components for my website and many require using MySQL INSERT, UPDATE, and DELETE commands. (I'm on Joomla 1.0.13 BTW). Right now, any queries that are SELECT work just fine. However, whenever I go to any of the above types, the query doesn't seem to execute, but there are no errors generated. Additionally, when I echo out the query statement and copy and paste it directly into my PHPMyAdmin, it executes just fine.

Any ideas what the problem might be? Let me know if you need code examples.
Last edited by jayred on Fri Jan 04, 2008 9:31 pm, edited 1 time in total.

User avatar
pe7er
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 162
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, The Netherlands
Contact:

Re: Problems with MySQL INSERT, UPDATE, and DELETE

Post by pe7er » Thu Jan 03, 2008 9:36 am

Joomla's default components do work okay?
e.g. Can you create (Insert record), Modify (Update record) or Delete (Delete record) an article?

Could you compare your code with code used in Joomla core components?
Kind Regards,
Peter Martin (aka pe7er)
db8.nl - Joomla! implementation, programming, template and component development [Dutch]
>> Questions? Get help more easily with JTS-post Assistant: viewtopic.php?f=428&t=272481

jayred
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sat Aug 11, 2007 7:02 am

Re: Problems with MySQL INSERT, UPDATE, and DELETE

Post by jayred » Thu Jan 03, 2008 9:52 am

Yes, all of the normal Joomla components work just fine. I can add, edit, and delete articles.

I also took a look at the code for the users core component, since I'm inserting / updating / deleting things in the users, core_acl, and contact_details tables, and double-checked that my code was the same syntax as the core code. And it definitely is.

Any ideas where to go from here?

User avatar
pe7er
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 162
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, The Netherlands
Contact:

Re: Problems with MySQL INSERT, UPDATE, and DELETE

Post by pe7er » Thu Jan 03, 2008 10:15 am

jayred wrote:I also took a look at the code for the users core component, since I'm inserting / updating / deleting things in the users, core_acl, and contact_details tables, and double-checked that my code was the same syntax as the core code.


Are you trying to insert/update/delete from multiple tables at the same time?

You can use select + join to retrieve info from multiple tables at the same time,
but I am not sure that you can do that with insert/update/delete.
(Note: maybe phpMyAdmin handles the requests differently, so there it might be possible)
Kind Regards,
Peter Martin (aka pe7er)
db8.nl - Joomla! implementation, programming, template and component development [Dutch]
>> Questions? Get help more easily with JTS-post Assistant: viewtopic.php?f=428&t=272481

User avatar
ircmaxell
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Thu Nov 10, 2005 3:10 am
Location: BumbleF&%K, NJ
Contact:

Re: Problems with MySQL INSERT, UPDATE, and DELETE

Post by ircmaxell » Thu Jan 03, 2008 2:15 pm

How are you executing the query? 

Code: Select all

$sql = "DELETE blah FROM blah";
$database->setQuery($sql);
if(!$database->query()) echo $database->getErrorMsg();
Joomla Development WorkGroup - Joomla BugSquad!
http://www.joomlaperformance.com For All Your Joomla Performance Needs
http://www.ircmaxell.com
The greatest obstacle to discovery is not ignorance, but the delusion of knowledge.

jayred
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sat Aug 11, 2007 7:02 am

Re: Problems with MySQL INSERT, UPDATE, and DELETE

Post by jayred » Thu Jan 03, 2008 11:59 pm

@pe7er:

No, I'm just trying to do one table.

@ircmaxwell:

Yep, that's how I'm executing the query and it's not throwing any errors.

Here's some of my code, in case it helps.

Code: Select all

global $database;

      $counter = 0;

      for( $i = 0; $i<count($positions); $i++ ){

         if( $positions[$i] != '' ){
            $query = "UPDATE #__contact_details"
            . "\n SET con_position = '{$positions[$i]}'"
            . "\n WHERE id = '{$cid[$counter]}'";
            $database->setQuery($query);
            if(!$database->query()) echo $database->getErrorMsg() . "<br/>";
            echo "$query <br/>";
            $counter++;
         }
      }

Where $cid is an array of contactIDs and $positions is an array of positions (duh). The echo produces the following for one example:

Code: Select all

UPDATE #__contact_details SET con_position = 'Please work' WHERE id = '194'

jayred
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sat Aug 11, 2007 7:02 am

Re: Problems with MySQL INSERT, UPDATE, and DELETE

Post by jayred » Fri Jan 04, 2008 6:17 am

I found the problem. Prior to ircmaxwell's comment with the code sample that included the if(!$database->query()), I did not have that line in my code. I was under the impression that the $database->setQuery($query) command performed the query since it did for "SELECT" statements (this is supposed to happen, right?). Well unfortunately, even after reviewing the documentation for the $database class in the Joomla docs, it still wasn't clear that I had to execute the query using $database->query().

Apparently, when I added that if(...) statement before making my last post with code sample, it worked, but somehow I missed that in fact it had and I thought it still didn't work.

So, to conclude, here's what I determined.

1. If performing a SELECT query, simply use $database->setQuery().

2. If performing an INSERT, UPDATE, or DELETE query, use $database->setQuery() followed by some form of $database->query()

Thank you both for your help.  :D

User avatar
pe7er
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 162
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, The Netherlands
Contact:

Re: Problems with MySQL INSERT, UPDATE, and DELETE

Post by pe7er » Fri Jan 04, 2008 7:52 am

Great, thanks for your follow-up with your solution!

Could you please mark your first message in this thread as "solved" ? (Modify the first message and choose the Message Icon [solved] Image).
This way the list of messages shows that the question has been solved, and other users can benefit from it.
Thanks!
Kind Regards,
Peter Martin (aka pe7er)
db8.nl - Joomla! implementation, programming, template and component development [Dutch]
>> Questions? Get help more easily with JTS-post Assistant: viewtopic.php?f=428&t=272481

User avatar
ircmaxell
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Thu Nov 10, 2005 3:10 am
Location: BumbleF&%K, NJ
Contact:

Re: Problems with MySQL INSERT, UPDATE, and DELETE

Post by ircmaxell » Fri Jan 04, 2008 1:53 pm

Actually, the way it works is that when you exectue a Select statement, one of the methods to load the data (like $database->loadObjectList()) actually executes $database->query()...  So if you're not loading the results, you need to exectue it manually...
Joomla Development WorkGroup - Joomla BugSquad!
http://www.joomlaperformance.com For All Your Joomla Performance Needs
http://www.ircmaxell.com
The greatest obstacle to discovery is not ignorance, but the delusion of knowledge.

jayred
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sat Aug 11, 2007 7:02 am

Re: Problems with MySQL INSERT, UPDATE, and DELETE

Post by jayred » Fri Jan 04, 2008 9:30 pm

Thanks for that explanation ircmaxwell. That's very clear and helpful. Thanks again for all of your help.


Post Reply