<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Blueline0</id>
	<title>Joomla! Documentation - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Blueline0"/>
	<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/Special:Contributions/Blueline0"/>
	<updated>2026-10-15T16:17:49Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Inserting,_Updating_and_Removing_data_using_JDatabase&amp;diff=999225</id>
		<title>Inserting, Updating and Removing data using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Inserting,_Updating_and_Removing_data_using_JDatabase&amp;diff=999225"/>
		<updated>2023-01-29T02:34:09Z</updated>

		<summary type="html">&lt;p&gt;Blueline0: Added $this-&amp;gt;getDatabse();&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;noinclude&amp;gt;&amp;lt;languages /&amp;gt;&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;{{Joomla version|version=3.x}}{{Joomla version|version=2.5|status=eos}}&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
{{-}}&lt;br /&gt;
{{tip|&amp;lt;translate&amp;gt;&amp;lt;!--T:1--&amp;gt;&lt;br /&gt;
Note many examples online use &amp;lt;code&amp;gt;$db-&amp;gt;query()&amp;lt;/code&amp;gt; instead of &amp;lt;code&amp;gt;$db-&amp;gt;execute()&amp;lt;/code&amp;gt;. This was the old method in Joomla 1.5 and 2.5 and will throw a deprecated notice in Joomla 3.0+.&amp;lt;/translate&amp;gt;|title=&amp;lt;translate&amp;gt;&amp;lt;!--T:2--&amp;gt;&lt;br /&gt;
Version Note&amp;lt;/translate&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:3--&amp;gt;&lt;br /&gt;
This tutorial is split into two independent parts:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:4--&amp;gt;&lt;br /&gt;
* Inserting, updating and removing data from the database.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:5--&amp;gt;&lt;br /&gt;
* Selecting data from one or more tables and retrieving it in a variety of forms.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:6--&amp;gt;&lt;br /&gt;
This section examines inserting, updating and removing data from a database table. See also the [[S:MyLanguage/Selecting_data_using_JDatabase|other part]].&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== Introduction== &amp;lt;!--T:7--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:8--&amp;gt;&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:9--&amp;gt;&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==The Query== &amp;lt;!--T:10--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:11--&amp;gt;&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &#039;&#039;query chaining&#039;&#039; is now the recommended method for building database queries (although string queries are still supported).&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:12--&amp;gt;&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:13--&amp;gt;&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;PHP&amp;quot;&amp;gt;&lt;br /&gt;
// Use one of the following depending on the file and version of Joomla &lt;br /&gt;
$db = $this-&amp;gt;_db; // use in Table class, also works in Model classes, J4 onwards&lt;br /&gt;
$db = $this-&amp;gt;getDatabase(); // only works in the Field, Model or Table classes, J4 onwards&lt;br /&gt;
&lt;br /&gt;
$db = JFactory::getDbo(); //deprecated J3, removed in J4 &lt;br /&gt;
$db = Factory::getDbo(); //deprecated J4 - requires use Joomla\CMS\Factory;&lt;br /&gt;
$db = $this-&amp;gt;getDbo(); // only works in the model or table classes, deprecated J5, use $this-&amp;gt;getDatabse();&lt;br /&gt;
$db = Factory::getContainer()-&amp;gt;get(&#039;DatabaseDriver&#039;); // Not well documented &lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:14--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;JDatabaseDriver::getQuery&amp;lt;/tt&amp;gt; takes an optional argument, &#039;&#039;$new&#039;&#039;, which can be true or false (the default being false).&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:15--&amp;gt;&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code.&amp;lt;/translate&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:16--&amp;gt;&lt;br /&gt;
Some of the more frequently used methods include: select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Inserting a Record== &amp;lt;!--T:17--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Using SQL=== &amp;lt;!--T:18--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:19--&amp;gt;&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being insert, columns, values.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo(); //deprecated J3, removed in J4 &lt;br /&gt;
$db = Factory::getDbo(); //deprecated J4 - requires use Joomla\CMS\Factory;&lt;br /&gt;
$db = $this-&amp;gt;getDbo(); // only works in the model or table classes, deprecated J4&lt;br /&gt;
$db = Factory::getContainer()-&amp;gt;get(&#039;DatabaseDriver&#039;); //J4 onwards, outside of the Model or Table  &lt;br /&gt;
$db = $this-&amp;gt;_db; //only works in the model or table classes, J4 onwards&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Set the query using our newly populated query object and execute it.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:42--&amp;gt;&lt;br /&gt;
(Here the &amp;lt;tt&amp;gt;quoteName()&amp;lt;/tt&amp;gt; function adds appropriate quotes around the table and column names to avoid conflicts with any database reserved word, now or in the future.)&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:54--&amp;gt;&lt;br /&gt;
To get the ID of the row that you just inserted, you can use the &#039;&#039;insertid&#039;&#039; method, for example.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get the row that was just inserted&lt;br /&gt;
$new_row_id = $db-&amp;gt;insertid();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
====How to Store Empty Value as NULL==== &amp;lt;!--T:55--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:56--&amp;gt;&lt;br /&gt;
If your default value of a column is NULL, you should not add that column name in the array. Let the database system store NULL as the default value.&lt;br /&gt;
If the default value of a column is not NULL and it is allowed to store NULL values, you should specify that in the code. See how to do it in the following example.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
/** First Case [NULL as default value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has NULL as default value. So, we will not add it to the array. The database engine will store NULL as value for column &#039;profile_value&#039;.&lt;br /&gt;
// $columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
/** Second Case [string as default value and you can also store NULL value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has empty string &#039;&#039; as default value but we can also store NULL value. So, we have to add the column name and NULL value to $columns and $values.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;NULL&#039;), 1);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Using an Object=== &amp;lt;!--T:20--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:21--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;JDatabaseDriver&amp;lt;/tt&amp;gt; class also provides a convenient method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
// Insert the object into the user profile table.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:22--&amp;gt;&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:23--&amp;gt;&lt;br /&gt;
The insertObject method will throw a error if there is a problem inserting the record into the database table.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:24--&amp;gt;&lt;br /&gt;
If you are providing a unique primary key value (as in the example above), it is highly recommended that you select from the table by that column value before attempting an insert.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:38--&amp;gt;&lt;br /&gt;
If you are simply inserting the next row in your table (i.e. the database generates a primary key value), you can specify the primary key column-name as the third parameter of the insertObject() method and the method will update the object with the newly generated primary key value.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:39--&amp;gt;&lt;br /&gt;
For example, given the following statement:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$result = $dbconnect-&amp;gt;insertObject(&#039;#__my_table&#039;, $object, &#039;primary_key&#039;); &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:40--&amp;gt;&lt;br /&gt;
after execution, $object-&amp;gt;primary_key will be updated with the newly inserted row&#039;s primary key value.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:41--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;HINT&#039;&#039;&#039; Set $object-&amp;gt;primary_key to null or 0 (zero) before inserting.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
====How to Store Empty Value as NULL When Inserting an Object==== &amp;lt;!--T:57--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:58--&amp;gt;&lt;br /&gt;
If your default value of a column is NULL, you should not add that column name to the object. Let the database system store NULL as the default value.&lt;br /&gt;
If the default value of a column is not NULL and it is allowed to store NULL values, you should specify that in the code. See how to do it in the following example.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
/** First Case [NULL as default value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has NULL as default value. So, we will not add it to the object. The database engine will store NULL as value for column &#039;profile_value&#039;.&lt;br /&gt;
// $profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
&lt;br /&gt;
/** Second Case [string as default value and you can also store NULL value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has empty string &#039;&#039; as default value but we can also store NULL value. So, we have to add the column name and NULL value as its value.&lt;br /&gt;
$profile-&amp;gt;profile_value = $db-&amp;gt;quote(&#039;NULL&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert the object into the user profile table.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==Updating a Record== &amp;lt;!--T:25--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Using SQL=== &amp;lt;!--T:26--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:27--&amp;gt;&lt;br /&gt;
The [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html JDatabaseQuery] class also provides methods for building update queries, in particular [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_update update] and [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_set set]. We also reuse another method which we used when creating select statements, the &#039;&#039;where&#039;&#039; method.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_value&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;Updating custom message for user 1001.&#039;),&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;ordering&#039;) . &#039; = 2&#039;,&lt;br /&gt;
&lt;br /&gt;
    // If you would like to store NULL value, you should specify that.&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;avatar&#039;) . &#039; = NULL&#039;,&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;user_id&#039;) . &#039; = 42&#039;, &lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_key&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;custom.message&#039;)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
$result = $db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
=== Using an Object === &amp;lt;!--T:28--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:29--&amp;gt;&lt;br /&gt;
Like &amp;lt;tt&amp;gt;insertObject&amp;lt;/tt&amp;gt;, the JDatabaseDriver class provides a convenience method for updating an object.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:30--&amp;gt;&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$updateNulls = true;&lt;br /&gt;
&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
// If you would like to store NULL value, you should specify that.&lt;br /&gt;
$object-&amp;gt;short_description = null;&lt;br /&gt;
&lt;br /&gt;
// Update their details in the users table using id as the primary key.&lt;br /&gt;
// You should provide forth parameter with value TRUE, if you would like to store the NULL values.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;, $updateNulls);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:31--&amp;gt;&lt;br /&gt;
Just like insertObject, updateObject takes care of escaping table names for us.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:32--&amp;gt;&lt;br /&gt;
The updateObject method will throw a error if there is a problem updating the record into the database table.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:33--&amp;gt;&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==Deleting a Record== &amp;lt;!--T:34--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:35--&amp;gt;&lt;br /&gt;
Finally, there is also a delete method to remove records from the database.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// delete all custom keys for user 1001.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;user_id&#039;) . &#039; = 1001&#039;, &lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_key&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;custom.%&#039;)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;delete($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;));&lt;br /&gt;
$query-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
$result = $db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== Sample Module Code == &amp;lt;!--T:43--&amp;gt;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:44--&amp;gt;&lt;br /&gt;
Below is the code for a simple Joomla module which you can install and run to demonstrate use of the JDatabase functionality for updating records in the database and which you can adapt to experiment with some of the concepts described above. If you are unsure about development and installing a Joomla module then following the tutorial at [[S:MyLanguage/J3.x:Creating a simple module/Introduction| Creating a simple module]] will help.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:45--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Important note: In any Joomla extensions which you develop that you should avoid accessing the core Joomla tables directly like this and should instead use the Joomla APIs if at all possible. The database structures may change without warning.&#039;&#039;&#039;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:46--&amp;gt;&lt;br /&gt;
In a folder mod_db_update create the following 2 files:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;mod_db_update.xml&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;extension type=&amp;quot;module&amp;quot; version=&amp;quot;3.1&amp;quot; client=&amp;quot;site&amp;quot; method=&amp;quot;upgrade&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;name&amp;gt;Database update demo&amp;lt;/name&amp;gt;&lt;br /&gt;
    &amp;lt;version&amp;gt;1.0.1&amp;lt;/version&amp;gt;&lt;br /&gt;
    &amp;lt;description&amp;gt;Code demonstrating use of Joomla Database class to perform SQL UPDATE statements&amp;lt;/description&amp;gt;&lt;br /&gt;
    &amp;lt;files&amp;gt;&lt;br /&gt;
        &amp;lt;filename module=&amp;quot;mod_db_update&amp;quot;&amp;gt;mod_db_update.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
    &amp;lt;/files&amp;gt;&lt;br /&gt;
&amp;lt;/extension&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;mod_db_update.php&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted Access&#039;);&lt;br /&gt;
&lt;br /&gt;
use Joomla\CMS\Factory;&lt;br /&gt;
&lt;br /&gt;
$db = Factory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$me = Factory::getUser();&lt;br /&gt;
&lt;br /&gt;
if ($me-&amp;gt;id == 0)&lt;br /&gt;
{&lt;br /&gt;
	echo &amp;quot;Not logged on!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
	$email = $me-&amp;gt;email; &lt;br /&gt;
	// change the case of the email address&lt;br /&gt;
	$email_uppercase = strtoupper($email);&lt;br /&gt;
	if ($email == $email_uppercase)&lt;br /&gt;
	{&lt;br /&gt;
		$new_email = strtolower($email);&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
	{&lt;br /&gt;
		$new_email = $email_uppercase;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
	$fields = array($db-&amp;gt;quoteName(&#039;email&#039;) . &amp;quot; = &#039;{$new_email}&#039;&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	$conditions = array($db-&amp;gt;quoteName(&#039;id&#039;) . &#039; = &#039; . $me-&amp;gt;id); &lt;br /&gt;
&lt;br /&gt;
	$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__users&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
	echo $db-&amp;gt;replacePrefix((string) $query);&lt;br /&gt;
	&lt;br /&gt;
	$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
	if ($result = $db-&amp;gt;execute())&lt;br /&gt;
	{&lt;br /&gt;
		echo &amp;quot;Email case successfully changed!&amp;quot;;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:47--&amp;gt;&lt;br /&gt;
The code above updates the email address field in the &amp;lt;tt&amp;gt;users&amp;lt;/tt&amp;gt; record of the currently logged-on user, toggling between upper case and lower case in successive reloads of the web page. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:48--&amp;gt;&lt;br /&gt;
The method &amp;lt;tt&amp;gt;Factory::getUser()&amp;lt;/tt&amp;gt; returns the &amp;lt;tt&amp;gt;user&amp;lt;/tt&amp;gt; object of the currently logged-on user, or if not logged on, then a blank &amp;lt;tt&amp;gt;user&amp;lt;/tt&amp;gt; object, whose &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; field is set to zero. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:49--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;$db-&amp;gt;replacePrefix((string) $query)&amp;lt;/tt&amp;gt; expression returns the actual SQL statement and outputting this can be useful in debugging. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:50--&amp;gt;&lt;br /&gt;
Zip up the &#039;&#039;mod_db_update&#039;&#039; directory to create &amp;lt;tt&amp;gt;mod_db_update.zip&amp;lt;/tt&amp;gt;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:51--&amp;gt;&lt;br /&gt;
Within your Joomla administrator go to Install Extensions and via the Upload Package File tab. Upload this zip file to install this sample log module.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:52--&amp;gt;&lt;br /&gt;
Make this module visible by editing it (click on it within the Modules page) then:&lt;br /&gt;
# Make its status &#039;&#039;Published&#039;&#039;&lt;br /&gt;
# Select a position on the page for it to be shown&lt;br /&gt;
# Specify the pages it should appear on in the Menu Assignment tab&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:53--&amp;gt;&lt;br /&gt;
When you visit a site web page then you should see the module in your selected position and it should output the SQL UPDATE statement and affirm that it has updated the record successfully. To confirm that it has updated the record correctly, go into PHPMyAdmin and view the &amp;lt;tt&amp;gt;users&amp;lt;/tt&amp;gt; table within the Joomla database. The updates aren&#039;t visible via the admin Users functionality on the backend, as Joomla displays in lower case all the email addresses in the records. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:36--&amp;gt;&lt;br /&gt;
[[Category:Database]]&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Blueline0</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Inserting,_Updating_and_Removing_data_using_JDatabase&amp;diff=995182</id>
		<title>Inserting, Updating and Removing data using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Inserting,_Updating_and_Removing_data_using_JDatabase&amp;diff=995182"/>
		<updated>2022-11-20T12:37:35Z</updated>

		<summary type="html">&lt;p&gt;Blueline0: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;noinclude&amp;gt;&amp;lt;languages /&amp;gt;&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;{{Joomla version|version=3.x}}{{Joomla version|version=2.5|status=eos}}&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
{{-}}&lt;br /&gt;
{{tip|&amp;lt;translate&amp;gt;&amp;lt;!--T:1--&amp;gt;&lt;br /&gt;
Note many examples online use &amp;lt;code&amp;gt;$db-&amp;gt;query()&amp;lt;/code&amp;gt; instead of &amp;lt;code&amp;gt;$db-&amp;gt;execute()&amp;lt;/code&amp;gt;. This was the old method in Joomla 1.5 and 2.5 and will throw a deprecated notice in Joomla 3.0+.&amp;lt;/translate&amp;gt;|title=&amp;lt;translate&amp;gt;&amp;lt;!--T:2--&amp;gt;&lt;br /&gt;
Version Note&amp;lt;/translate&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:3--&amp;gt;&lt;br /&gt;
This tutorial is split into two independent parts:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:4--&amp;gt;&lt;br /&gt;
* Inserting, updating and removing data from the database.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:5--&amp;gt;&lt;br /&gt;
* Selecting data from one or more tables and retrieving it in a variety of forms.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:6--&amp;gt;&lt;br /&gt;
This section examines inserting, updating and removing data from a database table. See also the [[S:MyLanguage/Selecting_data_using_JDatabase|other part]].&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== Introduction== &amp;lt;!--T:7--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:8--&amp;gt;&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:9--&amp;gt;&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==The Query== &amp;lt;!--T:10--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:11--&amp;gt;&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &#039;&#039;query chaining&#039;&#039; is now the recommended method for building database queries (although string queries are still supported).&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:12--&amp;gt;&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:13--&amp;gt;&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;PHP&amp;quot;&amp;gt;&lt;br /&gt;
// Use one of the following depending on the file and version of Joomla &lt;br /&gt;
$db = JFactory::getDbo(); //deprecated J3, removed in J4 &lt;br /&gt;
$db = Factory::getDbo(); //deprecated J4 - requires use Joomla\CMS\Factory;&lt;br /&gt;
$db = $this-&amp;gt;getDbo(); // only works in the model or table classes, deprecated J4&lt;br /&gt;
$db = Factory::getContainer()-&amp;gt;get(&#039;DatabaseDriver&#039;); //J4 onwards, outside of the Model or Table  &lt;br /&gt;
$db = $this-&amp;gt;_db; //only works in the model or table classes, J4 onwards&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:14--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;JDatabaseDriver::getQuery&amp;lt;/tt&amp;gt; takes an optional argument, &#039;&#039;$new&#039;&#039;, which can be true or false (the default being false).&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:15--&amp;gt;&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code.&amp;lt;/translate&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:16--&amp;gt;&lt;br /&gt;
Some of the more frequently used methods include: select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Inserting a Record== &amp;lt;!--T:17--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Using SQL=== &amp;lt;!--T:18--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:19--&amp;gt;&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being insert, columns, values.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo(); //deprecated J3, removed in J4 &lt;br /&gt;
$db = Factory::getDbo(); //deprecated J4 - requires use Joomla\CMS\Factory;&lt;br /&gt;
$db = $this-&amp;gt;getDbo(); // only works in the model or table classes, deprecated J4&lt;br /&gt;
$db = Factory::getContainer()-&amp;gt;get(&#039;DatabaseDriver&#039;); //J4 onwards, outside of the Model or Table  &lt;br /&gt;
$db = $this-&amp;gt;_db; //only works in the model or table classes, J4 onwards&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Set the query using our newly populated query object and execute it.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:42--&amp;gt;&lt;br /&gt;
(Here the &amp;lt;tt&amp;gt;quoteName()&amp;lt;/tt&amp;gt; function adds appropriate quotes around the table and column names to avoid conflicts with any database reserved word, now or in the future.)&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:54--&amp;gt;&lt;br /&gt;
To get the ID of the row that you just inserted, you can use the &#039;&#039;insertid&#039;&#039; method, for example.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get the row that was just inserted&lt;br /&gt;
$new_row_id = $db-&amp;gt;insertid();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
====How to Store Empty Value as NULL==== &amp;lt;!--T:55--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:56--&amp;gt;&lt;br /&gt;
If your default value of a column is NULL, you should not add that column name in the array. Let the database system store NULL as the default value.&lt;br /&gt;
If the default value of a column is not NULL and it is allowed to store NULL values, you should specify that in the code. See how to do it in the following example.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
/** First Case [NULL as default value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has NULL as default value. So, we will not add it to the array. The database engine will store NULL as value for column &#039;profile_value&#039;.&lt;br /&gt;
// $columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
/** Second Case [string as default value and you can also store NULL value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has empty string &#039;&#039; as default value but we can also store NULL value. So, we have to add the column name and NULL value to $columns and $values.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;NULL&#039;), 1);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Using an Object=== &amp;lt;!--T:20--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:21--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;JDatabaseDriver&amp;lt;/tt&amp;gt; class also provides a convenient method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
// Insert the object into the user profile table.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:22--&amp;gt;&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:23--&amp;gt;&lt;br /&gt;
The insertObject method will throw a error if there is a problem inserting the record into the database table.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:24--&amp;gt;&lt;br /&gt;
If you are providing a unique primary key value (as in the example above), it is highly recommended that you select from the table by that column value before attempting an insert.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:38--&amp;gt;&lt;br /&gt;
If you are simply inserting the next row in your table (i.e. the database generates a primary key value), you can specify the primary key column-name as the third parameter of the insertObject() method and the method will update the object with the newly generated primary key value.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:39--&amp;gt;&lt;br /&gt;
For example, given the following statement:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$result = $dbconnect-&amp;gt;insertObject(&#039;#__my_table&#039;, $object, &#039;primary_key&#039;); &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:40--&amp;gt;&lt;br /&gt;
after execution, $object-&amp;gt;primary_key will be updated with the newly inserted row&#039;s primary key value.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:41--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;HINT&#039;&#039;&#039; Set $object-&amp;gt;primary_key to null or 0 (zero) before inserting.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
====How to Store Empty Value as NULL When Inserting an Object==== &amp;lt;!--T:57--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:58--&amp;gt;&lt;br /&gt;
If your default value of a column is NULL, you should not add that column name to the object. Let the database system store NULL as the default value.&lt;br /&gt;
If the default value of a column is not NULL and it is allowed to store NULL values, you should specify that in the code. See how to do it in the following example.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
/** First Case [NULL as default value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has NULL as default value. So, we will not add it to the object. The database engine will store NULL as value for column &#039;profile_value&#039;.&lt;br /&gt;
// $profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
&lt;br /&gt;
/** Second Case [string as default value and you can also store NULL value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has empty string &#039;&#039; as default value but we can also store NULL value. So, we have to add the column name and NULL value as its value.&lt;br /&gt;
$profile-&amp;gt;profile_value = $db-&amp;gt;quote(&#039;NULL&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert the object into the user profile table.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==Updating a Record== &amp;lt;!--T:25--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Using SQL=== &amp;lt;!--T:26--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:27--&amp;gt;&lt;br /&gt;
The [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html JDatabaseQuery] class also provides methods for building update queries, in particular [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_update update] and [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_set set]. We also reuse another method which we used when creating select statements, the &#039;&#039;where&#039;&#039; method.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_value&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;Updating custom message for user 1001.&#039;),&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;ordering&#039;) . &#039; = 2&#039;,&lt;br /&gt;
&lt;br /&gt;
    // If you would like to store NULL value, you should specify that.&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;avatar&#039;) . &#039; = NULL&#039;,&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;user_id&#039;) . &#039; = 42&#039;, &lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_key&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;custom.message&#039;)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
$result = $db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
=== Using an Object === &amp;lt;!--T:28--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:29--&amp;gt;&lt;br /&gt;
Like &amp;lt;tt&amp;gt;insertObject&amp;lt;/tt&amp;gt;, the JDatabaseDriver class provides a convenience method for updating an object.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:30--&amp;gt;&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$updateNulls = true;&lt;br /&gt;
&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
// If you would like to store NULL value, you should specify that.&lt;br /&gt;
$object-&amp;gt;short_description = null;&lt;br /&gt;
&lt;br /&gt;
// Update their details in the users table using id as the primary key.&lt;br /&gt;
// You should provide forth parameter with value TRUE, if you would like to store the NULL values.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;, $updateNulls);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:31--&amp;gt;&lt;br /&gt;
Just like insertObject, updateObject takes care of escaping table names for us.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:32--&amp;gt;&lt;br /&gt;
The updateObject method will throw a error if there is a problem updating the record into the database table.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:33--&amp;gt;&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==Deleting a Record== &amp;lt;!--T:34--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:35--&amp;gt;&lt;br /&gt;
Finally, there is also a delete method to remove records from the database.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// delete all custom keys for user 1001.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;user_id&#039;) . &#039; = 1001&#039;, &lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_key&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;custom.%&#039;)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;delete($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;));&lt;br /&gt;
$query-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
$result = $db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== Sample Module Code == &amp;lt;!--T:43--&amp;gt;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:44--&amp;gt;&lt;br /&gt;
Below is the code for a simple Joomla module which you can install and run to demonstrate use of the JDatabase functionality for updating records in the database and which you can adapt to experiment with some of the concepts described above. If you are unsure about development and installing a Joomla module then following the tutorial at [[S:MyLanguage/J3.x:Creating a simple module/Introduction| Creating a simple module]] will help.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:45--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Important note: In any Joomla extensions which you develop that you should avoid accessing the core Joomla tables directly like this and should instead use the Joomla APIs if at all possible. The database structures may change without warning.&#039;&#039;&#039;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:46--&amp;gt;&lt;br /&gt;
In a folder mod_db_update create the following 2 files:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;mod_db_update.xml&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;extension type=&amp;quot;module&amp;quot; version=&amp;quot;3.1&amp;quot; client=&amp;quot;site&amp;quot; method=&amp;quot;upgrade&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;name&amp;gt;Database update demo&amp;lt;/name&amp;gt;&lt;br /&gt;
    &amp;lt;version&amp;gt;1.0.1&amp;lt;/version&amp;gt;&lt;br /&gt;
    &amp;lt;description&amp;gt;Code demonstrating use of Joomla Database class to perform SQL UPDATE statements&amp;lt;/description&amp;gt;&lt;br /&gt;
    &amp;lt;files&amp;gt;&lt;br /&gt;
        &amp;lt;filename module=&amp;quot;mod_db_update&amp;quot;&amp;gt;mod_db_update.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
    &amp;lt;/files&amp;gt;&lt;br /&gt;
&amp;lt;/extension&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;mod_db_update.php&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted Access&#039;);&lt;br /&gt;
&lt;br /&gt;
use Joomla\CMS\Factory;&lt;br /&gt;
&lt;br /&gt;
$db = Factory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$me = Factory::getUser();&lt;br /&gt;
&lt;br /&gt;
if ($me-&amp;gt;id == 0)&lt;br /&gt;
{&lt;br /&gt;
	echo &amp;quot;Not logged on!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
	$email = $me-&amp;gt;email; &lt;br /&gt;
	// change the case of the email address&lt;br /&gt;
	$email_uppercase = strtoupper($email);&lt;br /&gt;
	if ($email == $email_uppercase)&lt;br /&gt;
	{&lt;br /&gt;
		$new_email = strtolower($email);&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
	{&lt;br /&gt;
		$new_email = $email_uppercase;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
	$fields = array($db-&amp;gt;quoteName(&#039;email&#039;) . &amp;quot; = &#039;{$new_email}&#039;&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	$conditions = array($db-&amp;gt;quoteName(&#039;id&#039;) . &#039; = &#039; . $me-&amp;gt;id); &lt;br /&gt;
&lt;br /&gt;
	$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__users&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
	echo $db-&amp;gt;replacePrefix((string) $query);&lt;br /&gt;
	&lt;br /&gt;
	$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
	if ($result = $db-&amp;gt;execute())&lt;br /&gt;
	{&lt;br /&gt;
		echo &amp;quot;Email case successfully changed!&amp;quot;;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:47--&amp;gt;&lt;br /&gt;
The code above updates the email address field in the &amp;lt;tt&amp;gt;users&amp;lt;/tt&amp;gt; record of the currently logged-on user, toggling between upper case and lower case in successive reloads of the web page. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:48--&amp;gt;&lt;br /&gt;
The method &amp;lt;tt&amp;gt;Factory::getUser()&amp;lt;/tt&amp;gt; returns the &amp;lt;tt&amp;gt;user&amp;lt;/tt&amp;gt; object of the currently logged-on user, or if not logged on, then a blank &amp;lt;tt&amp;gt;user&amp;lt;/tt&amp;gt; object, whose &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; field is set to zero. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:49--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;$db-&amp;gt;replacePrefix((string) $query)&amp;lt;/tt&amp;gt; expression returns the actual SQL statement and outputting this can be useful in debugging. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:50--&amp;gt;&lt;br /&gt;
Zip up the &#039;&#039;mod_db_update&#039;&#039; directory to create &amp;lt;tt&amp;gt;mod_db_update.zip&amp;lt;/tt&amp;gt;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:51--&amp;gt;&lt;br /&gt;
Within your Joomla administrator go to Install Extensions and via the Upload Package File tab. Upload this zip file to install this sample log module.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:52--&amp;gt;&lt;br /&gt;
Make this module visible by editing it (click on it within the Modules page) then:&lt;br /&gt;
# Make its status &#039;&#039;Published&#039;&#039;&lt;br /&gt;
# Select a position on the page for it to be shown&lt;br /&gt;
# Specify the pages it should appear on in the Menu Assignment tab&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:53--&amp;gt;&lt;br /&gt;
When you visit a site web page then you should see the module in your selected position and it should output the SQL UPDATE statement and affirm that it has updated the record successfully. To confirm that it has updated the record correctly, go into PHPMyAdmin and view the &amp;lt;tt&amp;gt;users&amp;lt;/tt&amp;gt; table within the Joomla database. The updates aren&#039;t visible via the admin Users functionality on the backend, as Joomla displays in lower case all the email addresses in the records. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:36--&amp;gt;&lt;br /&gt;
[[Category:Database]]&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Blueline0</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Inserting,_Updating_and_Removing_data_using_JDatabase&amp;diff=995181</id>
		<title>Inserting, Updating and Removing data using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Inserting,_Updating_and_Removing_data_using_JDatabase&amp;diff=995181"/>
		<updated>2022-11-20T12:37:04Z</updated>

		<summary type="html">&lt;p&gt;Blueline0: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;noinclude&amp;gt;&amp;lt;languages /&amp;gt;&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;{{Joomla version|version=3.x}}{{Joomla version|version=2.5|status=eos}}&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
{{-}}&lt;br /&gt;
{{tip|&amp;lt;translate&amp;gt;&amp;lt;!--T:1--&amp;gt;&lt;br /&gt;
Note many examples online use &amp;lt;code&amp;gt;$db-&amp;gt;query()&amp;lt;/code&amp;gt; instead of &amp;lt;code&amp;gt;$db-&amp;gt;execute()&amp;lt;/code&amp;gt;. This was the old method in Joomla 1.5 and 2.5 and will throw a deprecated notice in Joomla 3.0+.&amp;lt;/translate&amp;gt;|title=&amp;lt;translate&amp;gt;&amp;lt;!--T:2--&amp;gt;&lt;br /&gt;
Version Note&amp;lt;/translate&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:3--&amp;gt;&lt;br /&gt;
This tutorial is split into two independent parts:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:4--&amp;gt;&lt;br /&gt;
* Inserting, updating and removing data from the database.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:5--&amp;gt;&lt;br /&gt;
* Selecting data from one or more tables and retrieving it in a variety of forms.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:6--&amp;gt;&lt;br /&gt;
This section examines inserting, updating and removing data from a database table. See also the [[S:MyLanguage/Selecting_data_using_JDatabase|other part]].&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== Introduction== &amp;lt;!--T:7--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:8--&amp;gt;&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:9--&amp;gt;&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==The Query== &amp;lt;!--T:10--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:11--&amp;gt;&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &#039;&#039;query chaining&#039;&#039; is now the recommended method for building database queries (although string queries are still supported).&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:12--&amp;gt;&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:13--&amp;gt;&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo(); //deprecated J3, removed in J4 &lt;br /&gt;
$db = Factory::getDbo(); //deprecated J4 - requires use Joomla\CMS\Factory;&lt;br /&gt;
$db = $this-&amp;gt;getDbo(); // only works in the model or table classes, deprecated J4&lt;br /&gt;
$db = Factory::getContainer()-&amp;gt;get(&#039;DatabaseDriver&#039;); //J4 onwards, outside of the Model or Table  &lt;br /&gt;
$db = $this-&amp;gt;_db; //only works in the model or table classes, J4 onwards&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:14--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;JDatabaseDriver::getQuery&amp;lt;/tt&amp;gt; takes an optional argument, &#039;&#039;$new&#039;&#039;, which can be true or false (the default being false).&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:15--&amp;gt;&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code.&amp;lt;/translate&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:16--&amp;gt;&lt;br /&gt;
Some of the more frequently used methods include: select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Inserting a Record== &amp;lt;!--T:17--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Using SQL=== &amp;lt;!--T:18--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:19--&amp;gt;&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being insert, columns, values.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo(); //deprecated J3, removed in J4 &lt;br /&gt;
$db = Factory::getDbo(); //deprecated J4 - requires use Joomla\CMS\Factory;&lt;br /&gt;
$db = $this-&amp;gt;getDbo(); // only works in the model or table classes, deprecated J4&lt;br /&gt;
$db = Factory::getContainer()-&amp;gt;get(&#039;DatabaseDriver&#039;); //J4 onwards, outside of the Model or Table  &lt;br /&gt;
$db = $this-&amp;gt;_db; //only works in the model or table classes, J4 onwards&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Set the query using our newly populated query object and execute it.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:42--&amp;gt;&lt;br /&gt;
(Here the &amp;lt;tt&amp;gt;quoteName()&amp;lt;/tt&amp;gt; function adds appropriate quotes around the table and column names to avoid conflicts with any database reserved word, now or in the future.)&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:54--&amp;gt;&lt;br /&gt;
To get the ID of the row that you just inserted, you can use the &#039;&#039;insertid&#039;&#039; method, for example.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get the row that was just inserted&lt;br /&gt;
$new_row_id = $db-&amp;gt;insertid();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
====How to Store Empty Value as NULL==== &amp;lt;!--T:55--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:56--&amp;gt;&lt;br /&gt;
If your default value of a column is NULL, you should not add that column name in the array. Let the database system store NULL as the default value.&lt;br /&gt;
If the default value of a column is not NULL and it is allowed to store NULL values, you should specify that in the code. See how to do it in the following example.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
/** First Case [NULL as default value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has NULL as default value. So, we will not add it to the array. The database engine will store NULL as value for column &#039;profile_value&#039;.&lt;br /&gt;
// $columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
/** Second Case [string as default value and you can also store NULL value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has empty string &#039;&#039; as default value but we can also store NULL value. So, we have to add the column name and NULL value to $columns and $values.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;NULL&#039;), 1);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Using an Object=== &amp;lt;!--T:20--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:21--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;JDatabaseDriver&amp;lt;/tt&amp;gt; class also provides a convenient method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
// Insert the object into the user profile table.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:22--&amp;gt;&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:23--&amp;gt;&lt;br /&gt;
The insertObject method will throw a error if there is a problem inserting the record into the database table.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:24--&amp;gt;&lt;br /&gt;
If you are providing a unique primary key value (as in the example above), it is highly recommended that you select from the table by that column value before attempting an insert.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:38--&amp;gt;&lt;br /&gt;
If you are simply inserting the next row in your table (i.e. the database generates a primary key value), you can specify the primary key column-name as the third parameter of the insertObject() method and the method will update the object with the newly generated primary key value.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:39--&amp;gt;&lt;br /&gt;
For example, given the following statement:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$result = $dbconnect-&amp;gt;insertObject(&#039;#__my_table&#039;, $object, &#039;primary_key&#039;); &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:40--&amp;gt;&lt;br /&gt;
after execution, $object-&amp;gt;primary_key will be updated with the newly inserted row&#039;s primary key value.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:41--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;HINT&#039;&#039;&#039; Set $object-&amp;gt;primary_key to null or 0 (zero) before inserting.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
====How to Store Empty Value as NULL When Inserting an Object==== &amp;lt;!--T:57--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:58--&amp;gt;&lt;br /&gt;
If your default value of a column is NULL, you should not add that column name to the object. Let the database system store NULL as the default value.&lt;br /&gt;
If the default value of a column is not NULL and it is allowed to store NULL values, you should specify that in the code. See how to do it in the following example.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
/** First Case [NULL as default value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has NULL as default value. So, we will not add it to the object. The database engine will store NULL as value for column &#039;profile_value&#039;.&lt;br /&gt;
// $profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
&lt;br /&gt;
/** Second Case [string as default value and you can also store NULL value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has empty string &#039;&#039; as default value but we can also store NULL value. So, we have to add the column name and NULL value as its value.&lt;br /&gt;
$profile-&amp;gt;profile_value = $db-&amp;gt;quote(&#039;NULL&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert the object into the user profile table.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==Updating a Record== &amp;lt;!--T:25--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Using SQL=== &amp;lt;!--T:26--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:27--&amp;gt;&lt;br /&gt;
The [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html JDatabaseQuery] class also provides methods for building update queries, in particular [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_update update] and [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_set set]. We also reuse another method which we used when creating select statements, the &#039;&#039;where&#039;&#039; method.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_value&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;Updating custom message for user 1001.&#039;),&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;ordering&#039;) . &#039; = 2&#039;,&lt;br /&gt;
&lt;br /&gt;
    // If you would like to store NULL value, you should specify that.&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;avatar&#039;) . &#039; = NULL&#039;,&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;user_id&#039;) . &#039; = 42&#039;, &lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_key&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;custom.message&#039;)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
$result = $db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
=== Using an Object === &amp;lt;!--T:28--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:29--&amp;gt;&lt;br /&gt;
Like &amp;lt;tt&amp;gt;insertObject&amp;lt;/tt&amp;gt;, the JDatabaseDriver class provides a convenience method for updating an object.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:30--&amp;gt;&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$updateNulls = true;&lt;br /&gt;
&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
// If you would like to store NULL value, you should specify that.&lt;br /&gt;
$object-&amp;gt;short_description = null;&lt;br /&gt;
&lt;br /&gt;
// Update their details in the users table using id as the primary key.&lt;br /&gt;
// You should provide forth parameter with value TRUE, if you would like to store the NULL values.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;, $updateNulls);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:31--&amp;gt;&lt;br /&gt;
Just like insertObject, updateObject takes care of escaping table names for us.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:32--&amp;gt;&lt;br /&gt;
The updateObject method will throw a error if there is a problem updating the record into the database table.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:33--&amp;gt;&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==Deleting a Record== &amp;lt;!--T:34--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:35--&amp;gt;&lt;br /&gt;
Finally, there is also a delete method to remove records from the database.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// delete all custom keys for user 1001.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;user_id&#039;) . &#039; = 1001&#039;, &lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_key&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;custom.%&#039;)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;delete($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;));&lt;br /&gt;
$query-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
$result = $db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== Sample Module Code == &amp;lt;!--T:43--&amp;gt;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:44--&amp;gt;&lt;br /&gt;
Below is the code for a simple Joomla module which you can install and run to demonstrate use of the JDatabase functionality for updating records in the database and which you can adapt to experiment with some of the concepts described above. If you are unsure about development and installing a Joomla module then following the tutorial at [[S:MyLanguage/J3.x:Creating a simple module/Introduction| Creating a simple module]] will help.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:45--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Important note: In any Joomla extensions which you develop that you should avoid accessing the core Joomla tables directly like this and should instead use the Joomla APIs if at all possible. The database structures may change without warning.&#039;&#039;&#039;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:46--&amp;gt;&lt;br /&gt;
In a folder mod_db_update create the following 2 files:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;mod_db_update.xml&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;extension type=&amp;quot;module&amp;quot; version=&amp;quot;3.1&amp;quot; client=&amp;quot;site&amp;quot; method=&amp;quot;upgrade&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;name&amp;gt;Database update demo&amp;lt;/name&amp;gt;&lt;br /&gt;
    &amp;lt;version&amp;gt;1.0.1&amp;lt;/version&amp;gt;&lt;br /&gt;
    &amp;lt;description&amp;gt;Code demonstrating use of Joomla Database class to perform SQL UPDATE statements&amp;lt;/description&amp;gt;&lt;br /&gt;
    &amp;lt;files&amp;gt;&lt;br /&gt;
        &amp;lt;filename module=&amp;quot;mod_db_update&amp;quot;&amp;gt;mod_db_update.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
    &amp;lt;/files&amp;gt;&lt;br /&gt;
&amp;lt;/extension&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;mod_db_update.php&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted Access&#039;);&lt;br /&gt;
&lt;br /&gt;
use Joomla\CMS\Factory;&lt;br /&gt;
&lt;br /&gt;
$db = Factory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$me = Factory::getUser();&lt;br /&gt;
&lt;br /&gt;
if ($me-&amp;gt;id == 0)&lt;br /&gt;
{&lt;br /&gt;
	echo &amp;quot;Not logged on!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
	$email = $me-&amp;gt;email; &lt;br /&gt;
	// change the case of the email address&lt;br /&gt;
	$email_uppercase = strtoupper($email);&lt;br /&gt;
	if ($email == $email_uppercase)&lt;br /&gt;
	{&lt;br /&gt;
		$new_email = strtolower($email);&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
	{&lt;br /&gt;
		$new_email = $email_uppercase;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
	$fields = array($db-&amp;gt;quoteName(&#039;email&#039;) . &amp;quot; = &#039;{$new_email}&#039;&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	$conditions = array($db-&amp;gt;quoteName(&#039;id&#039;) . &#039; = &#039; . $me-&amp;gt;id); &lt;br /&gt;
&lt;br /&gt;
	$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__users&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
	echo $db-&amp;gt;replacePrefix((string) $query);&lt;br /&gt;
	&lt;br /&gt;
	$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
	if ($result = $db-&amp;gt;execute())&lt;br /&gt;
	{&lt;br /&gt;
		echo &amp;quot;Email case successfully changed!&amp;quot;;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:47--&amp;gt;&lt;br /&gt;
The code above updates the email address field in the &amp;lt;tt&amp;gt;users&amp;lt;/tt&amp;gt; record of the currently logged-on user, toggling between upper case and lower case in successive reloads of the web page. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:48--&amp;gt;&lt;br /&gt;
The method &amp;lt;tt&amp;gt;Factory::getUser()&amp;lt;/tt&amp;gt; returns the &amp;lt;tt&amp;gt;user&amp;lt;/tt&amp;gt; object of the currently logged-on user, or if not logged on, then a blank &amp;lt;tt&amp;gt;user&amp;lt;/tt&amp;gt; object, whose &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; field is set to zero. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:49--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;$db-&amp;gt;replacePrefix((string) $query)&amp;lt;/tt&amp;gt; expression returns the actual SQL statement and outputting this can be useful in debugging. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:50--&amp;gt;&lt;br /&gt;
Zip up the &#039;&#039;mod_db_update&#039;&#039; directory to create &amp;lt;tt&amp;gt;mod_db_update.zip&amp;lt;/tt&amp;gt;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:51--&amp;gt;&lt;br /&gt;
Within your Joomla administrator go to Install Extensions and via the Upload Package File tab. Upload this zip file to install this sample log module.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:52--&amp;gt;&lt;br /&gt;
Make this module visible by editing it (click on it within the Modules page) then:&lt;br /&gt;
# Make its status &#039;&#039;Published&#039;&#039;&lt;br /&gt;
# Select a position on the page for it to be shown&lt;br /&gt;
# Specify the pages it should appear on in the Menu Assignment tab&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:53--&amp;gt;&lt;br /&gt;
When you visit a site web page then you should see the module in your selected position and it should output the SQL UPDATE statement and affirm that it has updated the record successfully. To confirm that it has updated the record correctly, go into PHPMyAdmin and view the &amp;lt;tt&amp;gt;users&amp;lt;/tt&amp;gt; table within the Joomla database. The updates aren&#039;t visible via the admin Users functionality on the backend, as Joomla displays in lower case all the email addresses in the records. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:36--&amp;gt;&lt;br /&gt;
[[Category:Database]]&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Blueline0</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Inserting,_Updating_and_Removing_data_using_JDatabase&amp;diff=995180</id>
		<title>Inserting, Updating and Removing data using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Inserting,_Updating_and_Removing_data_using_JDatabase&amp;diff=995180"/>
		<updated>2022-11-20T12:36:25Z</updated>

		<summary type="html">&lt;p&gt;Blueline0: Added some notes on the J4 $db&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;noinclude&amp;gt;&amp;lt;languages /&amp;gt;&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;{{Joomla version|version=3.x}}{{Joomla version|version=2.5|status=eos}}&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
{{-}}&lt;br /&gt;
{{tip|&amp;lt;translate&amp;gt;&amp;lt;!--T:1--&amp;gt;&lt;br /&gt;
Note many examples online use &amp;lt;code&amp;gt;$db-&amp;gt;query()&amp;lt;/code&amp;gt; instead of &amp;lt;code&amp;gt;$db-&amp;gt;execute()&amp;lt;/code&amp;gt;. This was the old method in Joomla 1.5 and 2.5 and will throw a deprecated notice in Joomla 3.0+.&amp;lt;/translate&amp;gt;|title=&amp;lt;translate&amp;gt;&amp;lt;!--T:2--&amp;gt;&lt;br /&gt;
Version Note&amp;lt;/translate&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:3--&amp;gt;&lt;br /&gt;
This tutorial is split into two independent parts:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:4--&amp;gt;&lt;br /&gt;
* Inserting, updating and removing data from the database.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:5--&amp;gt;&lt;br /&gt;
* Selecting data from one or more tables and retrieving it in a variety of forms.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:6--&amp;gt;&lt;br /&gt;
This section examines inserting, updating and removing data from a database table. See also the [[S:MyLanguage/Selecting_data_using_JDatabase|other part]].&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== Introduction== &amp;lt;!--T:7--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:8--&amp;gt;&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:9--&amp;gt;&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==The Query== &amp;lt;!--T:10--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:11--&amp;gt;&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &#039;&#039;query chaining&#039;&#039; is now the recommended method for building database queries (although string queries are still supported).&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:12--&amp;gt;&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:13--&amp;gt;&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:14--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;JDatabaseDriver::getQuery&amp;lt;/tt&amp;gt; takes an optional argument, &#039;&#039;$new&#039;&#039;, which can be true or false (the default being false).&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:15--&amp;gt;&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code.&amp;lt;/translate&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:16--&amp;gt;&lt;br /&gt;
Some of the more frequently used methods include: select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==Inserting a Record== &amp;lt;!--T:17--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Using SQL=== &amp;lt;!--T:18--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:19--&amp;gt;&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being insert, columns, values.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo(); //deprecated J3, removed in J4 &lt;br /&gt;
$db = Factory::getDbo(); //deprecated J4 - requires use Joomla\CMS\Factory;&lt;br /&gt;
$db = $this-&amp;gt;getDbo(); // only works in the model or table classes, deprecated J4&lt;br /&gt;
$db = Factory::getContainer()-&amp;gt;get(&#039;DatabaseDriver&#039;); //J4 onwards, outside of the Model or Table  &lt;br /&gt;
$db = $this-&amp;gt;_db; //only works in the model or table classes, J4 onwards&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Set the query using our newly populated query object and execute it.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:42--&amp;gt;&lt;br /&gt;
(Here the &amp;lt;tt&amp;gt;quoteName()&amp;lt;/tt&amp;gt; function adds appropriate quotes around the table and column names to avoid conflicts with any database reserved word, now or in the future.)&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:54--&amp;gt;&lt;br /&gt;
To get the ID of the row that you just inserted, you can use the &#039;&#039;insertid&#039;&#039; method, for example.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get the row that was just inserted&lt;br /&gt;
$new_row_id = $db-&amp;gt;insertid();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
====How to Store Empty Value as NULL==== &amp;lt;!--T:55--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:56--&amp;gt;&lt;br /&gt;
If your default value of a column is NULL, you should not add that column name in the array. Let the database system store NULL as the default value.&lt;br /&gt;
If the default value of a column is not NULL and it is allowed to store NULL values, you should specify that in the code. See how to do it in the following example.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
/** First Case [NULL as default value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has NULL as default value. So, we will not add it to the array. The database engine will store NULL as value for column &#039;profile_value&#039;.&lt;br /&gt;
// $columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
/** Second Case [string as default value and you can also store NULL value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has empty string &#039;&#039; as default value but we can also store NULL value. So, we have to add the column name and NULL value to $columns and $values.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;NULL&#039;), 1);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Using an Object=== &amp;lt;!--T:20--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:21--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;JDatabaseDriver&amp;lt;/tt&amp;gt; class also provides a convenient method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
// Insert the object into the user profile table.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:22--&amp;gt;&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:23--&amp;gt;&lt;br /&gt;
The insertObject method will throw a error if there is a problem inserting the record into the database table.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:24--&amp;gt;&lt;br /&gt;
If you are providing a unique primary key value (as in the example above), it is highly recommended that you select from the table by that column value before attempting an insert.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:38--&amp;gt;&lt;br /&gt;
If you are simply inserting the next row in your table (i.e. the database generates a primary key value), you can specify the primary key column-name as the third parameter of the insertObject() method and the method will update the object with the newly generated primary key value.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:39--&amp;gt;&lt;br /&gt;
For example, given the following statement:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$result = $dbconnect-&amp;gt;insertObject(&#039;#__my_table&#039;, $object, &#039;primary_key&#039;); &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:40--&amp;gt;&lt;br /&gt;
after execution, $object-&amp;gt;primary_key will be updated with the newly inserted row&#039;s primary key value.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:41--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;HINT&#039;&#039;&#039; Set $object-&amp;gt;primary_key to null or 0 (zero) before inserting.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
====How to Store Empty Value as NULL When Inserting an Object==== &amp;lt;!--T:57--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:58--&amp;gt;&lt;br /&gt;
If your default value of a column is NULL, you should not add that column name to the object. Let the database system store NULL as the default value.&lt;br /&gt;
If the default value of a column is not NULL and it is allowed to store NULL values, you should specify that in the code. See how to do it in the following example.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
/** First Case [NULL as default value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has NULL as default value. So, we will not add it to the object. The database engine will store NULL as value for column &#039;profile_value&#039;.&lt;br /&gt;
// $profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
&lt;br /&gt;
/** Second Case [string as default value and you can also store NULL value] **/&lt;br /&gt;
// The column &#039;profile_value&#039; has empty string &#039;&#039; as default value but we can also store NULL value. So, we have to add the column name and NULL value as its value.&lt;br /&gt;
$profile-&amp;gt;profile_value = $db-&amp;gt;quote(&#039;NULL&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert the object into the user profile table.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==Updating a Record== &amp;lt;!--T:25--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Using SQL=== &amp;lt;!--T:26--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:27--&amp;gt;&lt;br /&gt;
The [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html JDatabaseQuery] class also provides methods for building update queries, in particular [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_update update] and [https://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_set set]. We also reuse another method which we used when creating select statements, the &#039;&#039;where&#039;&#039; method.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_value&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;Updating custom message for user 1001.&#039;),&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;ordering&#039;) . &#039; = 2&#039;,&lt;br /&gt;
&lt;br /&gt;
    // If you would like to store NULL value, you should specify that.&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;avatar&#039;) . &#039; = NULL&#039;,&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;user_id&#039;) . &#039; = 42&#039;, &lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_key&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;custom.message&#039;)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
$result = $db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
=== Using an Object === &amp;lt;!--T:28--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:29--&amp;gt;&lt;br /&gt;
Like &amp;lt;tt&amp;gt;insertObject&amp;lt;/tt&amp;gt;, the JDatabaseDriver class provides a convenience method for updating an object.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:30--&amp;gt;&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$updateNulls = true;&lt;br /&gt;
&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
// If you would like to store NULL value, you should specify that.&lt;br /&gt;
$object-&amp;gt;short_description = null;&lt;br /&gt;
&lt;br /&gt;
// Update their details in the users table using id as the primary key.&lt;br /&gt;
// You should provide forth parameter with value TRUE, if you would like to store the NULL values.&lt;br /&gt;
$result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;, $updateNulls);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:31--&amp;gt;&lt;br /&gt;
Just like insertObject, updateObject takes care of escaping table names for us.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:32--&amp;gt;&lt;br /&gt;
The updateObject method will throw a error if there is a problem updating the record into the database table.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:33--&amp;gt;&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
==Deleting a Record== &amp;lt;!--T:34--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:35--&amp;gt;&lt;br /&gt;
Finally, there is also a delete method to remove records from the database.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// delete all custom keys for user 1001.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;user_id&#039;) . &#039; = 1001&#039;, &lt;br /&gt;
    $db-&amp;gt;quoteName(&#039;profile_key&#039;) . &#039; = &#039; . $db-&amp;gt;quote(&#039;custom.%&#039;)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;delete($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;));&lt;br /&gt;
$query-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
$result = $db-&amp;gt;execute();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== Sample Module Code == &amp;lt;!--T:43--&amp;gt;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:44--&amp;gt;&lt;br /&gt;
Below is the code for a simple Joomla module which you can install and run to demonstrate use of the JDatabase functionality for updating records in the database and which you can adapt to experiment with some of the concepts described above. If you are unsure about development and installing a Joomla module then following the tutorial at [[S:MyLanguage/J3.x:Creating a simple module/Introduction| Creating a simple module]] will help.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:45--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Important note: In any Joomla extensions which you develop that you should avoid accessing the core Joomla tables directly like this and should instead use the Joomla APIs if at all possible. The database structures may change without warning.&#039;&#039;&#039;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:46--&amp;gt;&lt;br /&gt;
In a folder mod_db_update create the following 2 files:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;mod_db_update.xml&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;extension type=&amp;quot;module&amp;quot; version=&amp;quot;3.1&amp;quot; client=&amp;quot;site&amp;quot; method=&amp;quot;upgrade&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;name&amp;gt;Database update demo&amp;lt;/name&amp;gt;&lt;br /&gt;
    &amp;lt;version&amp;gt;1.0.1&amp;lt;/version&amp;gt;&lt;br /&gt;
    &amp;lt;description&amp;gt;Code demonstrating use of Joomla Database class to perform SQL UPDATE statements&amp;lt;/description&amp;gt;&lt;br /&gt;
    &amp;lt;files&amp;gt;&lt;br /&gt;
        &amp;lt;filename module=&amp;quot;mod_db_update&amp;quot;&amp;gt;mod_db_update.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
    &amp;lt;/files&amp;gt;&lt;br /&gt;
&amp;lt;/extension&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;mod_db_update.php&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted Access&#039;);&lt;br /&gt;
&lt;br /&gt;
use Joomla\CMS\Factory;&lt;br /&gt;
&lt;br /&gt;
$db = Factory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$me = Factory::getUser();&lt;br /&gt;
&lt;br /&gt;
if ($me-&amp;gt;id == 0)&lt;br /&gt;
{&lt;br /&gt;
	echo &amp;quot;Not logged on!&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
	$email = $me-&amp;gt;email; &lt;br /&gt;
	// change the case of the email address&lt;br /&gt;
	$email_uppercase = strtoupper($email);&lt;br /&gt;
	if ($email == $email_uppercase)&lt;br /&gt;
	{&lt;br /&gt;
		$new_email = strtolower($email);&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
	{&lt;br /&gt;
		$new_email = $email_uppercase;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
	$fields = array($db-&amp;gt;quoteName(&#039;email&#039;) . &amp;quot; = &#039;{$new_email}&#039;&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	$conditions = array($db-&amp;gt;quoteName(&#039;id&#039;) . &#039; = &#039; . $me-&amp;gt;id); &lt;br /&gt;
&lt;br /&gt;
	$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__users&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
	echo $db-&amp;gt;replacePrefix((string) $query);&lt;br /&gt;
	&lt;br /&gt;
	$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
	if ($result = $db-&amp;gt;execute())&lt;br /&gt;
	{&lt;br /&gt;
		echo &amp;quot;Email case successfully changed!&amp;quot;;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:47--&amp;gt;&lt;br /&gt;
The code above updates the email address field in the &amp;lt;tt&amp;gt;users&amp;lt;/tt&amp;gt; record of the currently logged-on user, toggling between upper case and lower case in successive reloads of the web page. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:48--&amp;gt;&lt;br /&gt;
The method &amp;lt;tt&amp;gt;Factory::getUser()&amp;lt;/tt&amp;gt; returns the &amp;lt;tt&amp;gt;user&amp;lt;/tt&amp;gt; object of the currently logged-on user, or if not logged on, then a blank &amp;lt;tt&amp;gt;user&amp;lt;/tt&amp;gt; object, whose &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; field is set to zero. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:49--&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;$db-&amp;gt;replacePrefix((string) $query)&amp;lt;/tt&amp;gt; expression returns the actual SQL statement and outputting this can be useful in debugging. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:50--&amp;gt;&lt;br /&gt;
Zip up the &#039;&#039;mod_db_update&#039;&#039; directory to create &amp;lt;tt&amp;gt;mod_db_update.zip&amp;lt;/tt&amp;gt;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:51--&amp;gt;&lt;br /&gt;
Within your Joomla administrator go to Install Extensions and via the Upload Package File tab. Upload this zip file to install this sample log module.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:52--&amp;gt;&lt;br /&gt;
Make this module visible by editing it (click on it within the Modules page) then:&lt;br /&gt;
# Make its status &#039;&#039;Published&#039;&#039;&lt;br /&gt;
# Select a position on the page for it to be shown&lt;br /&gt;
# Specify the pages it should appear on in the Menu Assignment tab&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:53--&amp;gt;&lt;br /&gt;
When you visit a site web page then you should see the module in your selected position and it should output the SQL UPDATE statement and affirm that it has updated the record successfully. To confirm that it has updated the record correctly, go into PHPMyAdmin and view the &amp;lt;tt&amp;gt;users&amp;lt;/tt&amp;gt; table within the Joomla database. The updates aren&#039;t visible via the admin Users functionality on the backend, as Joomla displays in lower case all the email addresses in the records. &amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:36--&amp;gt;&lt;br /&gt;
[[Category:Database]]&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Blueline0</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=How_to_add_CSRF_anti-spoofing_to_forms&amp;diff=990912</id>
		<title>How to add CSRF anti-spoofing to forms</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=How_to_add_CSRF_anti-spoofing_to_forms&amp;diff=990912"/>
		<updated>2022-10-07T21:33:10Z</updated>

		<summary type="html">&lt;p&gt;Blueline0: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version|2.5,3.1,4.0}}&lt;br /&gt;
== What is a CSRF Attack? ==&lt;br /&gt;
A Cross Site Request Forgery ([[Wikipedia:Cross-site_request_forgery|CSRF]]) attack  relies on the trust a website has for a user to execute unauthorized requests and or transactions.  For example, say a user is logged into their Joomla! websites&#039; administrator interface in one tab and is browsing a compromised site in another tab.  A simple CSRF attack can be launched simply by tampering with IMG elements in some browsers so that they point to something like&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://some/joomla/site/administrator/index2.php?option=com_users&amp;amp;task=delete...&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
When the user browses the compromised site, that image will be requested and because the user is currently logged in to the administrator interface of her Joomla! site, the forged request will be positively authenticated and executed.  To prevent simple CSRF attacks like the one above, request tokens have been added to all forms in the front-end and back-end Joomla! interfaces.  The tokens are randomized strings that are used to authenticate that the request being made is coming from a valid form and a valid session.  This simple measure is very effective at preventing a large percentage of potential CSRF attacks, however, due to the nature of CSRF they are extremely difficult, if not impossible, to secure against completely.&lt;br /&gt;
&lt;br /&gt;
== Protecting Against CSRF Attacks ==&lt;br /&gt;
&lt;br /&gt;
Joomla! attempts to protect againt CSRF by inserting a random string called a token into each POST form and each GET query string that is able to modify something in the Joomla! system.  This random string provides protection because not only does the compromised site need to know the URL of the target site and a valid request format for the target site, it also must know the random string which changes for each session and each user.&lt;br /&gt;
&lt;br /&gt;
The {{jf}} makes it easy for you to include such protection in your components as well.  This is simple to implement in both POST and GET requests.&lt;br /&gt;
&lt;br /&gt;
=== POST Request ===&lt;br /&gt;
&lt;br /&gt;
POST requests are submitted in HTML using forms.  In order to add the token to your form, add the following line inside your form:&lt;br /&gt;
 &amp;lt;?php echo JHtml::_( &#039;form.token&#039; ); ?&amp;gt;&lt;br /&gt;
Joomla 4&lt;br /&gt;
 use Joomla\CMS\HTML\HTMLHelper;&lt;br /&gt;
 &amp;lt;?php echo HTMLHelper::_(&#039;form.token&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will output something like the following:&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;1234567890abcdef1234567890abcdef&amp;quot; value=&amp;quot;1&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== GET Request ===&lt;br /&gt;
&lt;br /&gt;
GET requests are submitted in HTML using query strings.  In order to add the token to your query string, use a URL like:&lt;br /&gt;
 &amp;lt;?php &lt;br /&gt;
 echo JRoute::_( &#039;index.php?option=com_example&amp;amp;controller=object1&amp;amp;task=save&amp;amp;&#039;. JSession::getFormToken() .&#039;=1&#039; ); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
Joomla 4&lt;br /&gt;
 use Joomla\CMS\Router\Route;&lt;br /&gt;
 use Joomla\CMS\Session\Session;&lt;br /&gt;
 &amp;lt;?php &lt;br /&gt;
 echo Route::_( &#039;index.php?option=com_example&amp;amp;controller=object1&amp;amp;task=save&amp;amp;&#039;. Session::getFormToken() .&#039;=1&#039; ); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate a URL with the token in the query string.&lt;br /&gt;
&lt;br /&gt;
=== Using JavaScript === &lt;br /&gt;
&lt;br /&gt;
 var csrf = Joomla.getOptions(&amp;quot;csrf.token&amp;quot;, &amp;quot;&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
=== Checking the Token ===&lt;br /&gt;
&lt;br /&gt;
Once you have included the token in your form or in your query string, you must check the token &#039;&#039;&#039;before your script carries out the request&#039;&#039;&#039;.  This is done with the following line:&lt;br /&gt;
 JSession::checkToken() or die(Text::_(&#039;JINVALID_TOKEN_NOTICE&#039;));&lt;br /&gt;
Joomla 4&lt;br /&gt;
 $this-&amp;gt;checkToken();&lt;br /&gt;
OR&lt;br /&gt;
 use Joomla\CMS\Session\Session;&lt;br /&gt;
 if(!Session::checkToken()) {&lt;br /&gt;
     throw new \Exception(Text::_(&#039;JINVALID_TOKEN_NOTICE&#039;), 403);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If the request is coming from the query string, you must specify this.  The code then becomes:&lt;br /&gt;
 JSession::checkToken( &#039;get&#039; ) or die( &#039;Invalid Token&#039; );&lt;br /&gt;
Joomla 4&lt;br /&gt;
 $this-&amp;gt;checkToken();&lt;br /&gt;
OR&lt;br /&gt;
 use Joomla\CMS\Session\Session;&lt;br /&gt;
 if(!Session::checkToken(&#039;get&#039;)) {&lt;br /&gt;
     throw new \Exception(Text::_(&#039;JINVALID_TOKEN_NOTICE&#039;), 403);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Recommended Security Procedures ==&lt;br /&gt;
&lt;br /&gt;
While these methods help to prevent against these types of attacks, it is important to realize that as a system administrator, there are good security practices to follow which will prevent a site from being compromised.&lt;br /&gt;
&lt;br /&gt;
# Don&#039;t browse other sites in the same browser while you are logged into your site.&lt;br /&gt;
# Log out from your site after you are done.&lt;br /&gt;
# Don&#039;t stay logged into your site while you are not doing anything.&lt;br /&gt;
# Ensure that the address in the browser bar matches the address of your site.&lt;br /&gt;
&lt;br /&gt;
By practicing these safe surfing habits you will eliminate most threats to your web site.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:FAQ]] [[Category:Security_FAQ]] [[Category:Security]] [[Category:Development]]&lt;br /&gt;
[[Category:Form fields]]&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Blueline0</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=How_to_add_CSRF_anti-spoofing_to_forms&amp;diff=990911</id>
		<title>How to add CSRF anti-spoofing to forms</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=How_to_add_CSRF_anti-spoofing_to_forms&amp;diff=990911"/>
		<updated>2022-10-07T21:30:46Z</updated>

		<summary type="html">&lt;p&gt;Blueline0: /* Checking the Token */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version|2.5,3.1,4.0}}&lt;br /&gt;
== What is a CSRF Attack? ==&lt;br /&gt;
A Cross Site Request Forgery ([[Wikipedia:Cross-site_request_forgery|CSRF]]) attack  relies on the trust a website has for a user to execute unauthorized requests and or transactions.  For example, say a user is logged into their Joomla! websites&#039; administrator interface in one tab and is browsing a compromised site in another tab.  A simple CSRF attack can be launched simply by tampering with IMG elements in some browsers so that they point to something like&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://some/joomla/site/administrator/index2.php?option=com_users&amp;amp;task=delete...&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
When the user browses the compromised site, that image will be requested and because the user is currently logged in to the administrator interface of her Joomla! site, the forged request will be positively authenticated and executed.  To prevent simple CSRF attacks like the one above, request tokens have been added to all forms in the front-end and back-end Joomla! interfaces.  The tokens are randomized strings that are used to authenticate that the request being made is coming from a valid form and a valid session.  This simple measure is very effective at preventing a large percentage of potential CSRF attacks, however, due to the nature of CSRF they are extremely difficult, if not impossible, to secure against completely.&lt;br /&gt;
&lt;br /&gt;
== Protecting Against CSRF Attacks ==&lt;br /&gt;
&lt;br /&gt;
Joomla! attempts to protect againt CSRF by inserting a random string called a token into each POST form and each GET query string that is able to modify something in the Joomla! system.  This random string provides protection because not only does the compromised site need to know the URL of the target site and a valid request format for the target site, it also must know the random string which changes for each session and each user.&lt;br /&gt;
&lt;br /&gt;
The {{jf}} makes it easy for you to include such protection in your components as well.  This is simple to implement in both POST and GET requests.&lt;br /&gt;
&lt;br /&gt;
=== POST Request ===&lt;br /&gt;
&lt;br /&gt;
POST requests are submitted in HTML using forms.  In order to add the token to your form, add the following line inside your form:&lt;br /&gt;
 &amp;lt;?php echo JHtml::_( &#039;form.token&#039; ); ?&amp;gt;&lt;br /&gt;
Joomla 4&lt;br /&gt;
 use Joomla\CMS\HTML\HTMLHelper;&lt;br /&gt;
 &amp;lt;?php echo HTMLHelper::_(&#039;form.token&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will output something like the following:&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;1234567890abcdef1234567890abcdef&amp;quot; value=&amp;quot;1&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== GET Request ===&lt;br /&gt;
&lt;br /&gt;
GET requests are submitted in HTML using query strings.  In order to add the token to your query string, use a URL like:&lt;br /&gt;
 &amp;lt;?php &lt;br /&gt;
 echo JRoute::_( &#039;index.php?option=com_example&amp;amp;controller=object1&amp;amp;task=save&amp;amp;&#039;. JSession::getFormToken() .&#039;=1&#039; ); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
Joomla 4&lt;br /&gt;
 use Joomla\CMS\Router\Route;&lt;br /&gt;
 use Joomla\CMS\Session\Session;&lt;br /&gt;
 &amp;lt;?php &lt;br /&gt;
 echo Route::_( &#039;index.php?option=com_example&amp;amp;controller=object1&amp;amp;task=save&amp;amp;&#039;. Session::getFormToken() .&#039;=1&#039; ); &lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will generate a URL with the token in the query string.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Checking the Token ===&lt;br /&gt;
&lt;br /&gt;
Once you have included the token in your form or in your query string, you must check the token &#039;&#039;&#039;before your script carries out the request&#039;&#039;&#039;.  This is done with the following line:&lt;br /&gt;
 JSession::checkToken() or die(Text::_(&#039;JINVALID_TOKEN_NOTICE&#039;));&lt;br /&gt;
Joomla 4&lt;br /&gt;
 $this-&amp;gt;checkToken();&lt;br /&gt;
OR&lt;br /&gt;
 use Joomla\CMS\Session\Session;&lt;br /&gt;
 if(!Session::checkToken()) {&lt;br /&gt;
     throw new \Exception(Text::_(&#039;JINVALID_TOKEN_NOTICE&#039;), 403);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If the request is coming from the query string, you must specify this.  The code then becomes:&lt;br /&gt;
 JSession::checkToken( &#039;get&#039; ) or die( &#039;Invalid Token&#039; );&lt;br /&gt;
Joomla 4&lt;br /&gt;
 $this-&amp;gt;checkToken();&lt;br /&gt;
OR&lt;br /&gt;
 use Joomla\CMS\Session\Session;&lt;br /&gt;
 if(!Session::checkToken(&#039;get&#039;)) {&lt;br /&gt;
     throw new \Exception(Text::_(&#039;JINVALID_TOKEN_NOTICE&#039;), 403);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Recommended Security Procedures ==&lt;br /&gt;
&lt;br /&gt;
While these methods help to prevent against these types of attacks, it is important to realize that as a system administrator, there are good security practices to follow which will prevent a site from being compromised.&lt;br /&gt;
&lt;br /&gt;
# Don&#039;t browse other sites in the same browser while you are logged into your site.&lt;br /&gt;
# Log out from your site after you are done.&lt;br /&gt;
# Don&#039;t stay logged into your site while you are not doing anything.&lt;br /&gt;
# Ensure that the address in the browser bar matches the address of your site.&lt;br /&gt;
&lt;br /&gt;
By practicing these safe surfing habits you will eliminate most threats to your web site.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:FAQ]] [[Category:Security_FAQ]] [[Category:Security]] [[Category:Development]]&lt;br /&gt;
[[Category:Form fields]]&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Blueline0</name></author>
	</entry>
</feed>