I want to add data in database from front end using modules. So I created a form as given below:
<form>
<div id="form_div">
<label>Title</label>
<input id="title" type="text" name="" />
<br /><br />
<label>File Name</label>
<input id="file_name" type="text" name="" />
<br /><br />
<label>Content</label>
<input id="content" type="text" name="" />
<br /><br />
<label>Status</label>
<input id="status" type="text" name="" />
<br /><br />
<input id="add_button" type="submit" value="Add" />
</div>
</form>
Can anyone suggest me how can I insert data from frontend in database
3 Answers 3
Put a action url in your form as shown below :
<form role="form" action="<?php echo $this->getUrl('modulefrontname/index/save'); ?>" method="post" autocomplete="off" id="form-validate-payment" enctype="multipart/form-data">
<?php echo $this->getBlockHtml('formkey')?>
<div id="form_div">
<label>Title</label>
<input id="title" type="text" name="title" />
<br /><br />
<label>File Name</label>
<input type="file" id="file_name" type="text" name="file_name" />
<br /><br />
<label>Content</label>
<input id="content" type="text" name="content" />
<br /><br />
<label>Status</label>
<input id="status" type="text" name="status" />
<br /><br />
<input id="add_button" type="submit" value="Add" />
</div>
</form>
Then create save action in indexController as shown below:
public function SaveAction() {
$postdata = $this->getRequest()->getPost();
if($postdata){
if (isset($_FILES['file_name']['name']) and (file_exists($_FILES['file_name']['tmp_name']))) {
$uploader = new Varien_File_Uploader('file_name');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . DS;
$uploader->save($path, $_FILES['file_name']['name']);
$filename = $_FILES['file_name']['name'];
}
$model = Mage::getModel("test/test");
$model->setData($postdata);
$model->setFileName($filename);
$model->save();
}
$this->loadLayout();
$this->renderLayout()
}
-
Thanks for the help. but in my case a new row is getting added but the data inside the table is not getting added.Birju– Birju2017年02月15日 08:40:55 +00:00Commented Feb 15, 2017 at 8:40
-
ok i got it, it worked. I forgot to add the name attribute. Thanks a lot @Prasanta HatuiBirju– Birju2017年02月15日 08:51:05 +00:00Commented Feb 15, 2017 at 8:51
You have to do the following steps to save data to dramatically.
- Register Module and Activate Module
- Create Controller
- Create Configuration XML
- Create Helper Class
- Create Models
- SQL Setup
- Template Design
- Blocks
You may be help the following article How to create and insert to a custom table in magento?
Create saveAction() in your controller, in thatsaveAction() put this code.
$model = Mage::getModel('test/test');
$data = array('dbfeildname1'=>$data1,'dbfeildname2'=>$data2);
$model->setData($data);
$model->save();
-
Thanks for help. But I want to add data when I click on submit button. How can I fire that?Birju– Birju2017年02月15日 08:08:36 +00:00Commented Feb 15, 2017 at 8:08
-
@Birju do you know how to create controller in magento ?2017年02月15日 08:09:49 +00:00Commented Feb 15, 2017 at 8:09
-
yes i have created a controller already where I have defined a function for delete beforeBirju– Birju2017年02月15日 08:10:48 +00:00Commented Feb 15, 2017 at 8:10
-
@Birju create saveAction() in controller for saveAction() and put this code in saveAction()2017年02月15日 08:12:49 +00:00Commented Feb 15, 2017 at 8:12