How to insert data into the joomla database table

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
dinakar
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Fri Dec 05, 2008 2:11 pm

How to insert data into the joomla database table

Postby dinakar » Wed Feb 04, 2009 11:53 am

i had created a table jos_mytable now i want to insert data into that table using joomla functions,
i had tried the following code
$db = JFactory::getDBO();
$query = "insert into #__mytable values('val1', 'val2', 'val3')";
$db->setQuery($query);

But this is not working for me;

User avatar
Macsimice
Joomla! Explorer
Joomla! Explorer
Posts: 332
Joined: Sun Mar 18, 2007 12:50 pm
Location: Netherlands
Contact:

Re: How to insert data into the joomla database table

Postby Macsimice » Wed Feb 04, 2009 12:06 pm

This should work:

Code: Select all

$data =new stdClass();
$data->id = null;
$data->field1 = 'val1';
$data->field2 = 'val2';
$data->field3 = 'val3';

$db = JFactory::getDBO();
$db->insertObject( '#__mytable', $data, id );


'id' is the name for your primary key, change that if you're using a different name. Of course you need to also change the name of your other fields.
Cheers, Babs
-------------------
http://crossinghippos.com

User avatar
Macsimice
Joomla! Explorer
Joomla! Explorer
Posts: 332
Joined: Sun Mar 18, 2007 12:50 pm
Location: Netherlands
Contact:

Re: How to insert data into the joomla database table

Postby Macsimice » Wed Feb 04, 2009 12:11 pm

dinakar wrote:$query = "insert into #__mytable values('val1', 'val2', 'val3')";


Incidently, your query syntax is off, it should be like:

Code: Select all

"INSERT INTO #__mytable (field1, field2, field3) VALUES ('val1', 'val2', 'val3')"
Cheers, Babs
-------------------
http://crossinghippos.com

User avatar
Macsimice
Joomla! Explorer
Joomla! Explorer
Posts: 332
Joined: Sun Mar 18, 2007 12:50 pm
Location: Netherlands
Contact:

Re: How to insert data into the joomla database table

Postby Macsimice » Wed Feb 04, 2009 12:12 pm

One more thing: check this page for more refs: http://api.joomla.org/Joomla-Framework/Table/JTable.html
Cheers, Babs
-------------------
http://crossinghippos.com

jchri66
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Sun Feb 15, 2009 3:37 am

Re: How to insert data into the joomla database table

Postby jchri66 » Fri Feb 20, 2009 6:10 am

I'd like to continue this thread since I am having the same problem. I posted here once already but it didn't seem to show up so forgive me I double post.
My SQL works directly against the MYSQL server just not through joomla.

$database = JFactory::getDBO();
$sql = "INSERT INTO jos_cust_pictures (PicturePath) VALUES ('c:pictures')";
$database->setQuery($sql);

I browsed through the last link and being new to php and joomla it didn't ring a bell on anything I'm doing wrong and I did try the other code suggested above.

One thing I do notice is that if I go to joomla and reclick my link to the code multiple times when I go back to the server and run the insert on the server no records will show from the joomla action but the auto-increment has incremented like it did.

So for example:
SQL run on the server gives me a record for autoID = 4.
If I click on my code in joomla 5 times and then go back to the server and run the query local again I will only see one additional record after 4 but that new record will have autoID = 10.
So MYSQL has to registering the hit but not taking the data.

Any ideas? Do I need to make sure that anything specific is included to make this work?

User avatar
dam-man
Joomla! Exemplar
Joomla! Exemplar
Posts: 7961
Joined: Fri Sep 09, 2005 2:13 pm
Location: The Netherlands
Contact:

Re: How to insert data into the joomla database table

Postby dam-man » Fri Feb 20, 2009 8:36 am

did you look into any documentation.
Joomla! has his own classes to insert/update queries.
Start here for example: viewtopic.php?f=231&t=136576
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards

jchri66
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Sun Feb 15, 2009 3:37 am

Re: How to insert data into the joomla database table

Postby jchri66 » Fri Feb 20, 2009 3:12 pm

Brother I didn't catch anything on that link that was lightbulb helpful. The link within the link that directs me to the where the thread has been redirected is also broken. Is the concept for the INSERT lengthy or can you help me out.

jchri66
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Sun Feb 15, 2009 3:37 am

Re: How to insert data into the joomla database table

Postby jchri66 » Fri Feb 20, 2009 7:48 pm

I did find this documentation:
http://help.joomla.org/content/view/712/125/

But that code still doesn't insert the row. I am able to create the DB object and look at properties like DB prefix so the object is good. Do I need to make reference to the table somewhere so Joomla knows about it? The table is a custom table.

jchri66
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Sun Feb 15, 2009 3:37 am

Re: How to insert data into the joomla database table

Postby jchri66 » Sat Feb 21, 2009 3:13 am

Never mind. I'm using SQL Manager 2007 Lite and the SELECT query only shows changes made in the console. So the joomla code was working all along I just couldn't see it in the MYSQL admin app I'm using. I must be missing something on the functionality of the app. After a close it and open it again the SELECT statement shows the records.

dinakar
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Fri Dec 05, 2008 2:11 pm

How to update a joomla table

Postby dinakar » Wed Mar 18, 2009 9:42 am

i had impleted the below code for updation a row in a table but row was not updated how can i do this

$db =& JFactory::getDBO();
$query = "UPDATE #__ecom_id_generate SET TABLE_NAME=$tableName WHERE ID_VALUE=$id";
$db->setQuery($query);
$db->query();

firecentaur
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri Mar 05, 2010 1:37 am

Re: How to insert data into the joomla database table

Postby firecentaur » Fri Mar 12, 2010 10:27 am

Thanks for the posts! How can I retrieve the ID of the new table row that has been created?

m_ragab333
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Sat Apr 10, 2010 4:19 pm

Re: How to insert data into the joomla database table

Postby m_ragab333 » Sat Apr 10, 2010 4:22 pm


rafi0
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Mon May 09, 2011 3:29 pm

Re: How to insert data into the joomla database table

Postby rafi0 » Mon May 09, 2011 3:34 pm

Thnkx for the post..:)

User avatar
mojito
Joomla! Guru
Joomla! Guru
Posts: 635
Joined: Wed Sep 07, 2005 10:18 pm
Location: London
Contact:

But how can we preserve html in the intro text field ?

Postby mojito » Wed May 11, 2011 12:13 pm

Hi
I want to preserve simple links and html in a field I am trying the following but it gets stripped to plain text no tags....

Code: Select all

$dataX =new stdClass();
...
$dataX->introtext = mysql_escape_string($data['introtext']);
...

$db->insertObject( '#__content', $dataX, id );


thanks
Check out the 'bad toilet' online !

User avatar
unitcn
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Wed Aug 31, 2011 8:57 am

Re: How to insert data into the joomla database table

Postby unitcn » Wed Aug 31, 2011 9:03 am

Testado no Joomla 1.6.6 e (MySQLi)... OK

$database =& JFactory::getDBO();
$sql = "INSERT INTO #__minhaTabela (meu_campo) VALUES ('valor')";
$database->setQuery($sql);
$database->query(); //<<< confirmação


Espero ter ajudado!

[PT-BR]

zarvan
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Mon Aug 29, 2011 12:55 pm

Re: How to insert data into the joomla database table

Postby zarvan » Wed Aug 31, 2011 9:18 am

Macsimice wrote:
dinakar wrote:$query = "insert into #__mytable values('val1', 'val2', 'val3')";


Incidently, your query syntax is off, it should be like:

Code: Select all

"INSERT INTO #__mytable (field1, field2, field3) VALUES ('val1', 'val2', 'val3')"


thanks Macsimice its very usefull fo me

antonitus
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Sat Feb 28, 2009 11:04 am
Location: London, UK

Re: How to insert data into the joomla database table

Postby antonitus » Wed Oct 31, 2012 11:44 pm

Can anybody tell me where in the MVC framework to add code to achieve the following as I'm quite new to the Joomla Development environment:

1. Read data from an existing table
2. Put that data in a new table
3. Output that data in a view page from the new table
4. Assign data to a unique user id in the new table

The data that is read and put in a the new table from an existing table must be connected, so for example if that data is deleted from the existing table, then the data in the new table should also be automatically deleted. I presume in MySQL speak that this is a JOIN or maybe not.

P.S. To the keen eyed person and admin, I've already mentioned a similar thread, but I didn't get any decent replies. I'm hoping this thread will be successful for me.

ra1
Joomla! Apprentice
Joomla! Apprentice
Posts: 43
Joined: Fri Aug 31, 2012 11:04 am

Re: How to insert data into the joomla database table

Postby ra1 » Sat Nov 03, 2012 11:55 am

hello evr1
i am trying to create a component in joomla 2.5 and i dont know how to get the data from for the <select>. i have created <select name="name"> in my default view and i want to insert that selected value in database, i know how to insert static value inside database using InsertObject as explaind by Macsimice and its working also but the null values coming inside the database ...
plz help me to store the selected value into database ..

antonitus
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Sat Feb 28, 2009 11:04 am
Location: London, UK

Re: How to insert data into the joomla database table

Postby antonitus » Tue Nov 06, 2012 7:30 pm

After many days of no response from anyone, especially a helpful developer, I don't think this is a good forum for Joomla talk. Stack Overflow forum is one of the best, at least you get a decent response.


Return to “Joomla! 1.5 Coding”

Who is online

Users browsing this forum: No registered users and 5 guests