Magento 1.9 – How to Save Data in Database

adminhtmlmagento-1.9

enter image description here

I have this form and I add new text field category id, I have a table where the data is saved. Now I create new column in table cat id and I want to save the cat id text field data in the table column this is my saveAction code

 if ($data = $this->getRequest()->getPost()) {
            if (isset($_FILES['image']['name']) and (file_exists($_FILES['image']['tmp_name']))) {
                try {
                    $uploader = new Varien_File_Uploader('image');
                    $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); // or pdf or anything

                    $uploader->setAllowRenameFiles(false);

                    $uploader->setFilesDispersion(false);

                    $path = Mage::getBaseDir('media') . DS . "campaign" . DS;

                    $uploader->save($path, $_FILES['image']['name']);

                    $data['image'] = $_FILES['image']['name'];
                } catch (Exception $e) {

                }
            } else {
                if (isset($data['image']['delete']) && $data['image']['delete'] == 1)
                    $data['image'] = '';
                else
                    unset($data['image']);
            }

            $model = Mage::getModel('campaign/campaign');
            $id = $this->getRequest()->getParam('id');

            if ($id) {
                $model->load($id);
            }

            $model->setData($data);

            Mage::getSingleton('adminhtml/session')->setFormData($data);
            try {
                if ($id) {
                    $model->setId($id);
                }

                $model->save();

                if (!$model->getId()) {
                    Mage::throwException(Mage::helper('campaign')->__('Error saving campaign details'));
                }

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('campaign')->__('Details was successfully saved.'));

                Mage::getSingleton('adminhtml/session')->setFormData(false);

                // The following line decides if it is a "save" or "save and continue"
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                } else {
                    $this->_redirect('*/*/');
                }
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                if ($model && $model->getId()) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                } else {
                    $this->_redirect('*/*/');
                }
            }
            return;
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('campaign')->__('No data found to save'));
        $this->_redirect('*/*/');
    }

Best Answer

simply add this line $data['cat_id']=$data['category_id']; before $model->setData($data); category_id will your field name and cat_id will be column name

 $model = Mage::getModel('campaign/campaign');
 $data['cat_id']=$data['category_id']; 
 $id = $this->getRequest()->getParam('id');

 if ($id) {
     $model->load($id);
 }

 $model->setData($data);

Note: after adding new column delete var/cache folder

Related Topic