Hi,
o.k., you're doing it the hard way (you won't benefit from abstraction if you hard code...). Joomla has a class called JTable and it is meant to be used for things like this. Here's a quick overview of what you should do:
1) create a JTable extension class having attributes matching those from your table.
e.g.
table:
Code: Select all
CREATE TABLE IF NOT EXISTS `#__smart_pictures`
(
`id` int(11) NOT NULL auto_increment,
`path` varchar(255),
`path_thumbnail` varchar(255),
`name` varchar(255),
`description` varchar(255),
`keywords` varchar(255),
`related_products` varchar(255),
PRIMARY KEY (`id`)
);
and JTable, located at admin/tables/pictures.php:
Code: Select all
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla table library
jimport('joomla.database.table');
/**
* Table class
*/
class SmartTablePictures extends JTable
{
/*primary key*/
var $id=null;
var $path=null;
var $path_thumbnail=null;
var $name=null;
var $description=null;
var $keywords=null;
var $related_products=null;
/**
* Constructor
*
* @param object Database connector object
*/
function __construct(&$db)
{
parent::__construct('#__smart_pictures', 'id', $db);
}
}
2) it's "better" if you place methods in controllers, so I would suggest you move the method to site/controller.php
Example:
Code: Select all
public function save_picture(){
//get id number
$id = JRequest::getVar('id', 0);
//variable for error counting
$error_count=0;
//create a database object instance
$type ='Pictures';
$prefix = 'SmartTable';
$config = array();
$row = JTable::getInstance($type, $prefix, $config);
if($id==null || $id='') {
//create picture
$row->reset();
}
else{
//edit picture
$row->load($id);
}
//associate the database object to the form
if (!$row->bind(JRequest::get('post'))) {
$error_count=$error_count+1;
}
if($error_count==0){
if (!$row->store()) {
JError::raiseWarning(100, $message1);
}
else {
$backlink = JRoute::_('index.php?option=' . $option . '&view=pictures' );
$this->setRedirect($backlink,JText::_('COM_SMART_PICTURE_ADDED'));
}
}
}
This is just a method in your class extension, not the whole file.
3) in your form assign the method's name to the task atribute, i.e., you should have something like this:
Code: Select all
<input type="hidden" name="id" value="<?php echo $this->item->id; ?>" />
<input type="hidden" name="task" value="save_picture" />
<input type="hidden" name="option" value="<?php echo (JRequest::getCmd('option')) ?>" />
<input type="submit" class="button" id="button" value="Aceptar"/>
Joomla tries to match the values in your table to those in the form, so make sure you use the same names. Refer to the MVC tutorials in docs.joomla.org/developers.
It might seem as overkill, but it's not that hard once you get used it.
Regards.