{"id":1692,"date":"2021-03-30T10:22:56","date_gmt":"2021-03-30T10:22:56","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/update-table-data-in-magento-2\/"},"modified":"2025-07-17T09:31:37","modified_gmt":"2025-07-17T04:01:37","slug":"update-table-data-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/update-table-data-in-magento-2\/","title":{"rendered":"How to Update Table Data in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">We already learned how to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/delete-data-from-table-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">delete data from table in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, if you want to&nbsp;update table data in&nbsp;Magento 2, do follow the below post.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once you add the record to a table, it often requires updating that data due to some reason. In that situation, it is not a good practice to delete that record and add it again. Moreover, that\u2019s time-consuming and frustrating too.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To overcome this kind of situation, use this method to update table data in\u00a0Magento 2\u00a0store and directly update the record by clicking on the \u2018edit\u2019 button.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Update Table Data in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Use the below code in the&nbsp;<strong>Showdata.php<\/strong>&nbsp;file at&nbsp;<strong><strong>app\/code\/Meetanshi\/Extension\/Block<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\Extension\\Block;\n\nuse Magento\\Framework\\View\\Element\\Template;\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Meetanshi\\Extension\\Model\\ResourceModel\\Extension\\CollectionFactory;\n\nclass Showdata extends Template\n{\n\n    public $collection;\n\n    public function __construct(Context $context, CollectionFactory $collectionFactory, array $data = [])\n    {\n        $this->collection = $collectionFactory;\n        parent::__construct($context, $data);\n    }\n\n    public function getCollection()\n    {\n        return $this->collection->create();\n    }\n\n    public function getDeleteAction()\n    {\n        return $this->getUrl('extension\/index\/delete', ['_secure' => true]);\n    }\n\n    public function getEditAction()\n    {\n        return $this->getUrl('extension\/index\/index', ['_secure' => true]);\n    }\n\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Use the below code in&nbsp;<strong>showdata.phtml&nbsp;<\/strong>at&nbsp;<strong><strong>app\/code\/Meetanshi\/Extension\/view\/frontend\/templates<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$collection = $block->getCollection();\nif ($collection->count()) {\n    ?>\n    &lt;div class=\"table-wrapper orders-history\">\n        &lt;table class=\"data table table-order-items history\" id=\"my-orders-table\">\n            &lt;caption class=\"table-caption\">&lt;?php echo __('Grid Record') ?>&lt;\/caption>\n            &lt;thead>\n            &lt;tr>\n                &lt;th scope=\"col\" class=\"col id\">&lt;?php echo __('ID') ?>&lt;\/th>\n                &lt;th scope=\"col\" class=\"col name\">&lt;?php echo __('Name') ?>&lt;\/th>\n                &lt;th scope=\"col\" class=\"col email\">&lt;?php echo __('Email') ?>&lt;\/th>\n                &lt;th scope=\"col\" class=\"col telephone\">&lt;?php echo __('Telephone') ?>&lt;\/th>\n                &lt;th scope=\"col\" class=\"col createat\">&lt;?php echo __('Created At') ?>&lt;\/th>\n                &lt;th scope=\"col\" class=\"col action\">&lt;?php echo __('Action') ?>&lt;\/th>\n            &lt;\/tr>\n            &lt;\/thead>\n            &lt;tbody>\n            &lt;?php\n            foreach ($collection as $item): ?>\n                &lt;tr>\n                    &lt;td data-th=\"&lt;?= $block->escapeHtml(__('ID')) ?>\" class=\"col id\">\n                        &lt;?php echo $item->getId() ?>\n                    &lt;\/td>\n                    &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Name')) ?>\" class=\"col name\">\n                        &lt;?php echo $item->getName() ?>\n                    &lt;\/td>\n                    &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Email')) ?>\" class=\"col email\">\n                        &lt;?php echo $item->getEmail() ?>\n                    &lt;\/td>\n                    &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Telephone')) ?>\" class=\"col telephone\">\n                        &lt;?php echo $item->getTelephone() ?>\n                    &lt;\/td>\n                    &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Created At')) ?>\"\n                        class=\"col title\">&lt;?php echo date('Y-m-d', strtotime($item->getCreatedAt())); ?>\n                    &lt;\/td>\n                    &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Action')) ?>\" class=\"col delete\">\n                        &lt;a href=\"&lt;?php echo $block->getDeleteAction() . 'id\/' . $item->getId(); ?>\">&lt;?php echo __('Delete') ?>&lt;\/a>\n                        &lt;a style=\"margin-left: 9px\"\n                           href=\"&lt;?php echo $block->getEditAction() . 'id\/' . $item->getId(); ?>\">&lt;?php echo __('Edit') ?>&lt;\/a>\n                    &lt;\/td>\n                &lt;\/tr>\n            &lt;?php endforeach; ?>\n            &lt;\/tbody>\n        &lt;\/table>\n    &lt;\/div>\n    &lt;?php\n}\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Use the below code in the&nbsp;<strong>Index.php&nbsp;<\/strong>file at&nbsp;<strong><strong>app\/code\/Meetanshi\/Extension\/Controller\/Index<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\Extension\\Controller\\Index;\n\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\Controller\\ResultFactory;\nuse Magento\\Framework\\UrlInterface;\nuse Magento\\Framework\\View\\Result\\PageFactory;\nuse Meetanshi\\Extension\\Model\\ExtensionFactory;\n\nclass Index extends Action\n{\n    protected $resultPageFactory;\n\n    private $extensionFactory;\n\n    private $url;\n\n    public function __construct(UrlInterface $url, ExtensionFactory $extensionFactory, Context $context, PageFactory $resultPageFactory)\n    {\n        parent::__construct($context);\n        $this->resultPageFactory = $resultPageFactory;\n        $this->extensionFactory = $extensionFactory;\n        $this->url = $url;\n    }\n\n    public function execute()\n    {\n        if ($this->isCorrectData()) {\n            return $this->resultPageFactory->create();\n        } else {\n            $this->messageManager->addErrorMessage(__(\"Record Not Found\"));\n            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);\n            $resultRedirect->setUrl($this->url->getUrl('extension\/index\/showdata'));\n            return $resultRedirect;\n        }\n    }\n\n    public function isCorrectData()\n    {\n        if ($id = $this->getRequest()->getParam(\"id\")) {\n            $model = $this->extensionFactory->create();\n            $model->load($id);\n            if ($model->getId()) {\n                return true;\n            } else {\n                return false;\n            }\n        } else {\n            return true;\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Paste the below code in the&nbsp;<strong>Form.php&nbsp;<\/strong>file at&nbsp;<strong><strong>app\/code\/Meetanshi\/Extension\/Block<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\Extension\\Block;\n\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Magento\\Framework\\View\\Element\\Template;\nuse Meetanshi\\Extension\\Model\\ExtensionFactory;\n\nclass Form extends Template\n{\n    private $extensionFactory;\n\n    public function __construct(ExtensionFactory $extensionFactory, Context $context, array $data = [])\n    {\n        parent::__construct($context, $data);\n        $this->extensionFactory = $extensionFactory;\n    }\n\n    public function getFormAction()\n    {\n        return $this->getUrl('extension\/index\/submit', ['_secure' => true]);\n    }\n\n    public function getAllData()\n    {\n        $id = $this->getRequest()->getParam(\"id\");\n        $model = $this->extensionFactory->create();\n        return $model->load($id);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">5. Use the below code in the&nbsp;<strong>form.phtml&nbsp;<\/strong>file at&nbsp;<strong><strong>app\/code\/Meetanshi\/Extension\/view\/frontend\/templates<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$data = $block->getAllData();\n$name = '';\n$email = '';\n$telephone = '';\nif ($data->getId()) {\n    $name = $data->getName();\n    $email = $data->getEmail();\n    $telephone = $data->getTelephone();\n}\n?>\n&lt;div class=\"row\">\n    &lt;div class=\"col-md-8\">\n        &lt;form name=\"addData\" method=\"post\" id=\"addData\" class=\"form\"\n              action=\"&lt;?php echo $this->getFormAction(); ?>\"\n              data-hasrequired=\"&lt;?= $block->escapeHtmlAttr(__('* Required Fields')) ?>\"\n              data-mage-init='{\"validation\":{}}'>\n            &lt;fieldset class=\"fieldset\">\n                &lt;legend class=\"legend\">&lt;span>&lt;?= $block->escapeHtmlAttr(__('Fill Detail')) ?>&lt;\/span>&lt;\/legend>\n                &lt;fieldset class=\"fieldset row\">\n                    &lt;?php\n                    if ($data->getId()) {\n                        ?>\n                        &lt;input type=\"hidden\" name=\"id\" value=\"&lt;?php echo $data->getId(); ?>\">\n                        &lt;?php\n                    } ?>\n                    &lt;div class=\"fields col-md-6\">\n                        &lt;div class=\"field name required\">\n                            &lt;label class=\"label\" for=\"title\">&lt;span>&lt;?= $block->\n                                    escapeHtmlAttr(__('Name')) ?>&lt;\/span>&lt;\/label>\n                            &lt;div class=\"control\">\n                                &lt;input name=\"name\" id=\"name\" title=\"Name\" value=\"&lt;?php echo $name ?>\" class=\"input-text\"\n                                       type=\"text\"\n                                       data-validate=\"{required:true, 'validate-alphanum-with-spaces':true}\">\n                            &lt;\/div>\n                        &lt;\/div>\n                        &lt;div class=\"field name required\">\n                            &lt;label class=\"label\" for=\"title\">&lt;span>&lt;?= $block->\n                                    escapeHtmlAttr(__('Email')) ?>&lt;\/span>&lt;\/label>\n                            &lt;div class=\"control\">\n                                &lt;input name=\"email\" id=\"email\" title=\"Email\" value=\"&lt;?php echo $email; ?>\"\n                                       class=\"input-text\" type=\"text\"\n                                       data-validate=\"{required:true, 'validate-email':true}\">\n                            &lt;\/div>\n                        &lt;\/div>\n                        &lt;div class=\"field name required\">\n                            &lt;label class=\"label\" for=\"title\">&lt;span>&lt;?= $block->escapeHtmlAttr(__('Telephone')) ?>&lt;\/span>&lt;\/label>\n                            &lt;div class=\"control\">\n                                &lt;input name=\"telephone\" id=\"telephone\" title=\"Telephone\"\n                                       value=\"&lt;?php echo $telephone; ?>\" class=\"input-text\"\n                                       type=\"text\" data-validate=\"{required:true}\">\n                            &lt;\/div>\n                        &lt;\/div>\n                    &lt;\/div>\n                &lt;\/fieldset>\n            &lt;\/fieldset>\n            &lt;div class=\"actions-toolbar\">\n                &lt;div class=\"primary\">\n                    &lt;button type=\"submit\" class=\"action submit primary\" title=\"Save\">&lt;span>&lt;?= $block->\n                            escapeHtmlAttr(__('Save')) ?>&lt;\/span>&lt;\/button>\n                &lt;\/div>\n            &lt;\/div>\n        &lt;\/form>\n    &lt;\/div>\n&lt;\/div><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">6. Use the below code in the&nbsp;<strong>Submit.php&nbsp;<\/strong>file at&nbsp;<strong><strong>app\/code\/Meetanshi\/Extension\/Controller\/Index<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\Extension\\Controller\\Index;\n\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\Controller\\ResultFactory;\nuse Magento\\Framework\\UrlInterface;\nuse Magento\\Framework\\View\\Result\\PageFactory;\nuse Meetanshi\\Extension\\Model\\ExtensionFactory;\n\nclass Submit extends Action\n{\n    protected $resultPageFactory;\n    protected $extensionFactory;\n    private $url;\n\n    public function __construct(\n        UrlInterface $url,\n        Context $context,\n        PageFactory $resultPageFactory,\n        ExtensionFactory $extensionFactory\n    )\n    {\n        $this->resultPageFactory = $resultPageFactory;\n        $this->extensionFactory = $extensionFactory;\n        $this->url = $url;\n        parent::__construct($context);\n    }\n\n    public function execute()\n    {\n        try {\n            $data = (array)$this->getRequest()->getPost();\n\n            if ($data) {\n                $model = $this->extensionFactory->create();\n                $model->setData($data)->save();\n                $this->messageManager->addSuccessMessage(__(\"Data Saved Successfully.\"));\n            }\n        } catch (\\Exception $e) {\n            $this->messageManager->addErrorMessage($e, __(\"We can\\'t submit your request, Please try again.\"));\n        }\n        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);\n        $resultRedirect->setUrl($this->url->getUrl('extension\/index\/showdata'));\n        return $resultRedirect;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once you\u2019ve set the above code, the \u2018Edit\u2019 action column will be added to the table as shown below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-at-March-30th-2021-12.00.41-pm.png\" alt=\"How to Update Table Data in Magento 2\" class=\"wp-image-14185\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The edit form will be open after clicking on the \u2018Edit\u2019 button.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-at-March-30th-2021-12.01.28-pm.png\" alt=\"How to Update Table Data in Magento 2\" class=\"wp-image-14187\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Once you update the data, it shows a message that notifies you a message as shown below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2021\/03\/Screenshot-at-March-30th-2021-12.00.16-pm.png\" alt=\"How to Update Table Data in Magento 2\" class=\"wp-image-14191\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Done!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also read:<\/strong>&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/alternative-to-magento-2-deprecated-load-save-and-delete-methods\/\" target=\"_blank\" rel=\"noreferrer noopener\">Alternative to Magento 2 Deprecated Load, Save and Delete Methods<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Read more:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/meetanshi.com\/blog\/display-table-data-magento-2\/\">How to Display Table Data in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/delete-data-from-table-in-magento-2\/\">How to Delete Data from Table in Magento 2<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We already learned how to&nbsp;delete data from table in Magento 2. Now, if you want to&nbsp;update table data in&nbsp;Magento 2, do follow the below post&#8230;.<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1692","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1692","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1692"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1692\/revisions"}],"predecessor-version":[{"id":18156,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1692\/revisions\/18156"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}