When you are doing simple single table inserts and updates you can
use the JDatabase methods to do so. The advantages of using the
JDatabase methods is it saves your time from hand coding sql and
will escape and correctly quote your inputs for you.
The following will insert a new record into a table:
Here is more info on using JTable.
The following will insert a new record into a table:
<?php
$db = JFactory::getDBO();
//Create data object
$row = new JObject();
$row->title = 'The Title';
$row->author = 'Bob Smith';
//Insert new record into #__book table.
$ret = $db->insertObject('#__book', $row);
//Get the new record id
$new_id = (int)$db->insertid();
?>
This will update an existing record:<?php
$db = JFactory::getDBO();
//Create data object
$row = new JObject();
//Record to update
$row->rec_id = 200;
$row->title = 'The Title';
$row->author = 'Bob Smith';
//Update the record. Third parameter is table id field that will be used to update.
$ret = $db->updateObject('#__book', $row,'rec_id');
?>
You can also use JTable to insert and update records, but requires
more initial setup since you have to create a new JTable class for each
table you want to modify. Using JTable is preferred if table has many
fields to update from a form submit. JTable will automatically bind the
form fields to corresponding table fields using the bind() method.Here is more info on using JTable.
HI, Like your article, i wan to save update and delete data from frontend example i hve employee table , want to edit its detail etc , waht to do ???any idea??
ReplyDeleteI have read your blog its very attractive and impressive. I like it your blog.
PHP Training in chennai | PHP Training Course
PHP Training in chennai | Online PHP Course