How to insert data into the joomla database table
-
- Joomla! Apprentice
- Posts: 18
- Joined: Fri Dec 05, 2008 2:11 pm
How to insert data into the joomla database table
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;
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;
- Macsimice
- Joomla! Explorer
- Posts: 331
- Joined: Sun Mar 18, 2007 12:50 pm
- Location: Netherlands
- Contact:
Re: How to insert data into the joomla database table
This should work:
'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.
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 );
- Macsimice
- Joomla! Explorer
- Posts: 331
- Joined: Sun Mar 18, 2007 12:50 pm
- Location: Netherlands
- Contact:
Re: How to insert data into the joomla database table
Incidently, your query syntax is off, it should be like:dinakar wrote:$query = "insert into #__mytable values('val1', 'val2', 'val3')";
Code: Select all
"INSERT INTO #__mytable (field1, field2, field3) VALUES ('val1', 'val2', 'val3')"
- Macsimice
- Joomla! Explorer
- Posts: 331
- Joined: Sun Mar 18, 2007 12:50 pm
- Location: Netherlands
- Contact:
Re: How to insert data into the joomla database table
One more thing: check this page for more refs: http://api.joomla.org/Joomla-Framework/ ... Table.html
-
- Joomla! Apprentice
- Posts: 8
- Joined: Sun Feb 15, 2009 3:37 am
Re: How to insert data into the joomla database table
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?
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?
- dam-man
- 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
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
Joomla! has his own classes to insert/update queries.
Start here for example: http://forum.joomla.org/viewtopic.php?f=231&t=136576
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards
Dutch Boards | Joomla Coding Boards | English Support Boards
-
- Joomla! Apprentice
- Posts: 8
- Joined: Sun Feb 15, 2009 3:37 am
Re: How to insert data into the joomla database table
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.
-
- Joomla! Apprentice
- Posts: 8
- Joined: Sun Feb 15, 2009 3:37 am
Re: How to insert data into the joomla database table
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.
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.
-
- Joomla! Apprentice
- Posts: 8
- Joined: Sun Feb 15, 2009 3:37 am
Re: How to insert data into the joomla database table
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.
-
- Joomla! Apprentice
- Posts: 18
- Joined: Fri Dec 05, 2008 2:11 pm
How to update a joomla table
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();
$db =& JFactory::getDBO();
$query = "UPDATE #__ecom_id_generate SET TABLE_NAME=$tableName WHERE ID_VALUE=$id";
$db->setQuery($query);
$db->query();
-
- Joomla! Fledgling
- Posts: 2
- Joined: Fri Mar 05, 2010 1:37 am
Re: How to insert data into the joomla database table
Thanks for the posts! How can I retrieve the ID of the new table row that has been created?
-
- Joomla! Fledgling
- Posts: 1
- Joined: Sat Apr 10, 2010 4:19 pm
Re: How to insert data into the joomla database table
$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]
$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]
-
- Joomla! Fledgling
- Posts: 1
- Joined: Mon May 09, 2011 3:29 pm
Re: How to insert data into the joomla database table
Thnkx for the post..
- mojito
- Joomla! Guru
- Posts: 755
- Joined: Wed Sep 07, 2005 10:18 pm
- Location: London
- Contact:
But how can we preserve html in the intro text field ?
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....
thanks
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 );
I am a freelance SEO (https://cambs.eu) web designer and developer working with Wordpress and Joomla since Mambo.
- unitcn
- Joomla! Fledgling
- Posts: 1
- Joined: Wed Aug 31, 2011 8:57 am
Re: How to insert data into the joomla database table
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]
$database =& JFactory::getDBO();
$sql = "INSERT INTO #__minhaTabela (meu_campo) VALUES ('valor')";
$database->setQuery($sql);
$database->query(); //<<< confirmação
Espero ter ajudado!
[PT-BR]
-
- Joomla! Apprentice
- Posts: 20
- Joined: Mon Aug 29, 2011 12:55 pm
Re: How to insert data into the joomla database table
thanks Macsimice its very usefull fo meMacsimice wrote:Incidently, your query syntax is off, it should be like:dinakar wrote:$query = "insert into #__mytable values('val1', 'val2', 'val3')";Code: Select all
"INSERT INTO #__mytable (field1, field2, field3) VALUES ('val1', 'val2', 'val3')"
-
- 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
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.
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.
-
- Joomla! Apprentice
- Posts: 43
- Joined: Fri Aug 31, 2012 11:04 am
Re: How to insert data into the joomla database table
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 ..
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 ..
-
- 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
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.