Page 1 of 1

How to insert data into the joomla database table

Posted: Wed Feb 04, 2009 11:53 am
by dinakar
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;

Re: How to insert data into the joomla database table

Posted: Wed Feb 04, 2009 12:06 pm
by Macsimice
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.

Re: How to insert data into the joomla database table

Posted: Wed Feb 04, 2009 12:11 pm
by Macsimice
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')"

Re: How to insert data into the joomla database table

Posted: Wed Feb 04, 2009 12:12 pm
by Macsimice
One more thing: check this page for more refs: http://api.joomla.org/Joomla-Framework/ ... Table.html

Re: How to insert data into the joomla database table

Posted: Fri Feb 20, 2009 6:10 am
by jchri66
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?

Re: How to insert data into the joomla database table

Posted: Fri Feb 20, 2009 8:36 am
by dam-man
did you look into any documentation.
Joomla! has his own classes to insert/update queries.
Start here for example: http://forum.joomla.org/viewtopic.php?f=231&t=136576

Re: How to insert data into the joomla database table

Posted: Fri Feb 20, 2009 3:12 pm
by jchri66
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.

Re: How to insert data into the joomla database table

Posted: Fri Feb 20, 2009 7:48 pm
by jchri66
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.

Re: How to insert data into the joomla database table

Posted: Sat Feb 21, 2009 3:13 am
by jchri66
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.

How to update a joomla table

Posted: Wed Mar 18, 2009 9:42 am
by dinakar
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();

Re: How to insert data into the joomla database table

Posted: Fri Mar 12, 2010 10:27 am
by firecentaur
Thanks for the posts! How can I retrieve the ID of the new table row that has been created?

Re: How to insert data into the joomla database table

Posted: Sat Apr 10, 2010 4:22 pm
by m_ragab333
$db =& JFactory::getDBO();
$query = "SELECT userid FROM #__acctexp_subscr where type != 'free' and userid = '$get_logged_user_id' ";
$db->setQuery($query);
$result = $db->loadObjectList();

foreach($result as $result_value){
foreach($result_value as $result_value_2){
$result_value_2.'<br />';

}

}

my name is Mohamed Ragab Dahab
email: [email protected]

Re: How to insert data into the joomla database table

Posted: Mon May 09, 2011 3:34 pm
by rafi0
Thnkx for the post..:)

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

Posted: Wed May 11, 2011 12:13 pm
by mojito
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

Re: How to insert data into the joomla database table

Posted: Wed Aug 31, 2011 9:03 am
by unitcn
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]

Re: How to insert data into the joomla database table

Posted: Wed Aug 31, 2011 9:18 am
by zarvan
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

Re: How to insert data into the joomla database table

Posted: Wed Oct 31, 2012 11:44 pm
by antonitus
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.

Re: How to insert data into the joomla database table

Posted: Sat Nov 03, 2012 11:55 am
by ra1
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 ..

Re: How to insert data into the joomla database table

Posted: Tue Nov 06, 2012 7:30 pm
by antonitus
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.