I am posting the Code here please help.
Every time i run this a new row is created, but no data is saved in it, except the id.Is there any geters and setters in it.
Trying to insert a value in the Database using post method. Its saving a new row into the table, but the value is not saved. I don't know what happened.
<?php
// No direct access
defined('_JEXEC') or die;
jimport('joomla.application.component.controller');
class NewbookController extends JControllerLegacy
{
public static function getHello($params)
{
$db =JFactory::getDBO();
echo $name = $_POST['name'];
echo $img =$_POST['image'];
$query = "INSERT INTO `book_newbook` (`id`,`name`,`image`)
VALUES ('','$name', '$img');";
$db->setQuery( $query );
$db->query();
//$result = $db->loadResult();
//return $result;
}
}
?>
<form action="index.php" name="adminForm" method="post" enctype="multipart/form-data">
<p>Name :
<input type="text" name="name"/>
</p>
<p>Image :
<input type="file" name="image"/>
</p>
<p>
<input type="submit" value="OK" name="ok"/>
</p>
</form>
To get you started on a few things:
$input = new JInput;
$name = $input->get('name', '', 'post');
$img = $input->get('image', '', 'post');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$columns = array('id', 'name', 'image');
$values = array($db->quote($name), $db->quote($image));
$query->insert($db->quoteName('#__book_newbook'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
$db->query();
Note: see the prefix before the database table >> #__book_newbook
Please bare in mind this is something to get you started. You still need to have a read of Developing an MVC Component for Joomla so that everything is up to scratch.
Hope this helps
Assuming your db table key is 'id'
, you are inserting this as ''
, i.e. blank. this will seldom work if at all. Replace with
"INSERT INTO `book_newbook` (`name`,`image`)
VALUES ('$name', '$img');";
and ID should be updated automatically.
Regarding updating a database with Joomla, read up on their standards for database queries as they have their own way of doing things - http://docs.joomla.org/Accessing_the_database_using_JDatabase