|
This short snippet demonstrates how to insert data into a joomla database table using the built in $database global object. Note that you have to declare the variable if you are making this call from within a function. If this call is being made from within a component or module, then declaring the variable is not required.
For Joomla 1.5 For Joomla 1.5 the insertObject() and updateObject() functions can be used. insertObject() For inserting data, set the primary key in the stdClass object to null, then denote the same primary key field name as the third argument in the insertObject function. The insertObject function's first argument is the table name, the second is the stdClass object containing the name of the fields as the keys and the value you want to insert on the right hand side of the assignment and as I mentioned before, the third argument is the name of the primary key field, assuming it is an auto_increment in mysql, you can set it to null, and the object will create a new incremented id for you.
updateObject() The updateObject accepts the same number of arguments, the only important thing to note here is that in the stdClass object, the primary key field is set to the ID of the record that you are trying to update. So figure out what the ID is and set it to that numeric value.
Get the Last inserted ID If you are creating a new record, and want to get the ID of the last inserted record, assuming that the table is set to auto increment on the PK field, call the insertid() method after you do the insert.
|