Today we discuss the Magento 2 save data in the custom table.
How to insert data into a custom table using the model.
And update data into database by sending id of the data.
You can send data clicking on button and reload the page.
If you do not want refresh or reload the page during the insert data, update data and detele data then you can use Ajax method.
See the code example below, which will help you save/edit/delete data.
Save data without Ajax request
Create a table using schema.xml
File Path: app/code/Custom/UserForm/etc/db_schema.xml
<?xml version="1.0" ?> <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd"> <table name="custom_userform_userform" resource="default" engine="innodb" comment="custom_userform_userform Table"> <column xsi:type="int" name="userform_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity Id"/> <constraint xsi:type="primary" referenceId="PRIMARY"> <column name="userform_id"/> </constraint> <column name="fname" nullable="true" xsi:type="text" comment="fname"/> <column name="lname" nullable="true" xsi:type="text" comment="lname"/> </table> </schema>
HTML form
<form class="form form-login" action="" method="post"> First Name <input type="text" name="fname" class="form-control" > Last Name <input type="text" name="lname" class="form-control" > <input type="hidden" name="userid" class="form-control" data-bind="value: userid"> <button class="action primary" name="savedata">Save</button> </form>
Create controller save.php file
Path: app/code/Custom/UserForm/Controller/Index/Save.php
<?php namespace Custom\UserForm\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Framework\View\Result\PageFactory; use Custom\UserForm\Model\UserFormFactory; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\App\Action\Action; class Save extends Action { protected $resultPageFactory; protected $userFormFactory; public function __construct( Context $context, PageFactory $resultPageFactory, UserFormFactory $userFormFactory ) { $this->resultPageFactory = $resultPageFactory; $this->userFormFactory = $userFormFactory; parent::__construct($context); } public function execute() { try { $data = (array)$this->getRequest()->getPost(); if ($data) { $model = $this->userFormFactory->create(); // for the update data if(isset($data['id'])) { $model->load($data['id']); } $model->setData($data)->save(); $this->messageManager->addSuccessMessage(__("Data Saved Successfully.")); } } catch (\Exception $e) { $this->messageManager->addErrorMessage($e, __("We can\'t submit your request, Please try again.")); } $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setUrl($this->_redirect->getRefererUrl()); return $resultRedirect; } }
Save data using with ajax request
<?php namespace Custom\UserForm\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Framework\View\Result\PageFactory; use Custom\UserForm\Model\UserFormFactory; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\App\Action\Action; class Save extends Action { protected $resultPageFactory; protected $userFormFactory; protected $jsonResultFactory; public function __construct( Context $context, PageFactory $resultPageFactory, \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory, UserFormFactory $userFormFactory ) { $this->resultPageFactory = $resultPageFactory; $this->userFormFactory = $userFormFactory; $this->jsonResultFactory = $jsonResultFactory; parent::__construct($context); } public function execute() { $resultJson = $this->jsonResultFactory->create(); try { $pdata = (array)$this->getRequest()->getPost(); if ($pdata) { $model = $this->userFormFactory->create(); $data['fname'] = $pdata['fname']; $data['lname'] = $pdata['lname']; // for the update data if($pdata['userid']) { $data['userform_id'] = $pdata['userid']; $model->load($data['userform_id']); } $model->setData($data)->save(); $this->messageManager->addSuccessMessage(__("Data Saved Successfully.")); $returndata['success']='success'; }else{ $returndata['nodata'] = 'no data'; } } catch (\Exception $e) { $returndata['error']= $e->getMessage(); //$this->messageManager->addErrorMessage($e, __("We can\'t submit your request, Please try again.")); } $result = $resultJson->setData($returndata); return $result; } }
Delete record from table
$model = $this->userFormFactory->create(); $model->load($data['id']); $model->delete(); $this->messageManager->addSuccessMessage(__("Record Delete Successfully."));
magento 2 save data in custom table
Like us on Facebook and Linkedin for more updates.
Related : Magento 2 create category attribute using upgradeData