1

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

asked Feb 15, 2017 at 8:02
0

3 Answers 3

1

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()
}
answered Feb 15, 2017 at 8:17
2
  • Thanks for the help. but in my case a new row is getting added but the data inside the table is not getting added. Commented Feb 15, 2017 at 8:40
  • ok i got it, it worked. I forgot to add the name attribute. Thanks a lot @Prasanta Hatui Commented Feb 15, 2017 at 8:51
1

You have to do the following steps to save data to dramatically.

  1. Register Module and Activate Module
  2. Create Controller
  3. Create Configuration XML
  4. Create Helper Class
  5. Create Models
  6. SQL Setup
  7. Template Design
  8. Blocks

You may be help the following article How to create and insert to a custom table in magento?

answered Feb 15, 2017 at 8:42
1

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(); 
answered Feb 15, 2017 at 8:05
4
  • Thanks for help. But I want to add data when I click on submit button. How can I fire that? Commented Feb 15, 2017 at 8:08
  • @Birju do you know how to create controller in magento ? Commented Feb 15, 2017 at 8:09
  • yes i have created a controller already where I have defined a function for delete before Commented Feb 15, 2017 at 8:10
  • @Birju create saveAction() in controller for saveAction() and put this code in saveAction() Commented Feb 15, 2017 at 8:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.