0

I have created a custom module for seller management in magento 1.7.0.2. When I try to edit and save an entry in that module, I get a SQL syntax error.


This is the error I get

enter image description here


This is the code of my Form.php

protected function _prepareForm()
{
    $form = new Varien_Data_Form(array(
        'id' => 'edit_form',
        'action' => $this->getUrl(
            'managesellers/index/save',
            array(
                '_current' => true,
                'continue' => 0,
            )
        ),
        'method' => 'post',
    ));


    $form->setUseContainer(true);
    $this->setForm($form);


    $fieldset = $form->addFieldset(
        'general',
        array(
            'legend' => $this->__('Seller Details')
        )
    );
     $this->_addFieldsToFieldset($fieldset, array(
        'managesellers_id' => array(
            'label' => $this->__('ID'),
            'input' => 'text',
            'required' => false,
        ),

    ));
    $this->_addFieldsToFieldset($fieldset, array(
        'name' => array(
            'label' => $this->__('Name'),
            'input' => 'text',
            'required' => true,
        ),

    ));
     $this->_addFieldsToFieldset($fieldset, array(
        'address' => array(
            'label' => $this->__('Address'),
            'input' => 'text',
            'required' => true,
        ),

    ));
     $this->_addFieldsToFieldset($fieldset, array(
        'shipping_charges' => array(
            'label' => $this->__('Shipping Charges'),
            'input' => 'text',
            'required' => true,
        ),

    ));
    $this->_addFieldsToFieldset($fieldset, array(
        'min_value' => array(
            'label' => $this->__('Minimum Value'),
            'input' => 'text',
            'required' => true,
        ),

    ));

    //To fill the edit form with data
    $id = Mage::app()->getRequest()->getParam('managesellers_id');
    $model= Mage::getModel('managesellers/managesellers')->load($id);
    $form->setValues($model->getData());

    return $this;
}

protected function _addFieldsToFieldset(
    Varien_Data_Form_Element_Fieldset $fieldset, $fields)
{
    $requestData = new Varien_Object($this->getRequest()
        ->getPost('managesellersData'));

    foreach ($fields as $name => $_data) {
        if ($requestValue = $requestData->getData($name)) {
            $_data['value'] = $requestValue;
        }
        $_data['name'] = "managesellersData[$name]";
        $fieldset->addField($name, $_data['input'], $_data);
    }

    return $this;
}

This is my Controller.php

public function saveAction() 
{
    if( $this->getRequest()->getParam('managesellers_id') > 0 ) 
    {
        try 
        {
            $id=$this->getRequest()->getParam('managesellers_id');
            $model = Mage::getModel('managesellers/managesellers');
            $model->setId($this->getRequest()->getParam('managesellers_id'))->save();

            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
            $this->_redirect('*/*/');
        }
        catch (Exception $e) 
        {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            $this->_redirect('*/*/edit', array('managesellers_id' => $this->getRequest()->getParam('managesellers_id')));
        }
    }
    $this->_redirect('*/*/');
}

How can I solve this error. Also, if there is any SQL queries to be written, where should I write those ?

Please help.

Thanks in advance :)

Update 1: Code Added

Update 2: Problem Solved

The problem was with the form which I created. I changed the code of my form.php & saveAction() method in controller.php and got it solved.

Form.php

public function __construct()
{
    parent::__construct();

    $this->_id = 'edit_form';
    $this->_title = $this->__('Seller Information');
}
protected function _prepareForm()
{
    $form = new Varien_Data_Form(array(
        'id' => 'edit_form',
        'action' => $this->getUrl(
            'managesellers/index/save',
            array(
                '_current' => true,
                'continue' => 0,
            )
        ),
        'method' => 'post',
    ));


    $form->setUseContainer(true);
    $this->setForm($form);
    $fieldset = $form->addFieldset('edit_form', array('legend'=>Mage::helper('managesellers')->__('Seller information')));

    $fieldset->addField('name', 'text', array(
        'label'     => Mage::helper('managesellers')->__('name'),
        'class'     => 'required-entry',
        'required'  => true,
        'name'      => 'name',
    ));
    $fieldset->addField('managesellers_id', 'text', array(
        'label'     => Mage::helper('managesellers')->__('managesellers_id'),
        'class'     => 'required-entry',
        'required'  => true,
        'name'      => 'managesellers_id',
    ));
    $fieldset->addField('address', 'text', array(
        'label'     => Mage::helper('managesellers')->__('address'),
        'class'     => 'required-entry',
        'required'  => true,
        'name'      => 'address',
    ));

    $fieldset->addField('shipping_charges', 'text', array(
        'label'     => Mage::helper('managesellers')->__('shipping_charges'),
        'class'     => 'required-entry',
        'required'  => true,
        'name'      => 'shipping_charges',
    ));
    $fieldset->addField('min_value', 'text', array(
        'label'     => Mage::helper('managesellers')->__('min_value'),
        'class'     => 'required-entry',
        'required'  => true,
        'name'      => 'min_value',
     ));

    if ( Mage::getSingleton('adminhtml/session')->getmanagesellersData() )
    {
        $form->setValues(Mage::getSingleton('adminhtml/session')->getmanagesellersData());
        Mage::getSingleton('adminhtml/session')->setmanagesellersData(null);
    }
    else if ( Mage::registry('managesellers_data')) 
    {
        $form->setValues(Mage::registry('managesellers_data')->getData());
    }

    return $this;
}    

saveAction() method

public function saveAction() 
{
    if($this->getRequest()->getParam('managesellers_id') > 0 ) 
    {
        try 
        {
            $id = $this->getRequest()->getParam('managesellers_id');
            $postData = $this->getRequest()->getPost();
            $model = Mage::getModel('managesellers/managesellers')->load($id);
            if ($model->isObjectNew() == false) 
            {
                $model->setData($postData);
                $model->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
            } 
            else 
            {
                $model->setData($postData);
                $model->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
            }
        }
        catch (Exception $e) 
        {   
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            $this->_redirect('*/*/edit', array('managesellers_id' => $this->getRequest()->getParam('managesellers_id')));
        }
    }
    $this->_redirect('*/*/');
}
1

Update your code from

$model->setId($this->getRequest()->getParam('managesellers_id'))->save(); 

To

$model->setData($this->getRequest()->getPost())->setId($this->getRequest()->getParam('managesellers_id'))->save(); 
  • it didn't work :( – Rajat Agarwal Feb 6 '17 at 5:19
  • Solved the problem. Added the answer to the question :) – Rajat Agarwal Feb 6 '17 at 6:23
0

This looks related to your other question (https://magento.stackexchange.com/questions/158117/save-button-saves-empty-row-in-edit-form/158160#158160) for which I posted:

There looks to be 3 reasons for whats happening:

  1. The form may not be populating the managesellers_id field correctly.
  2. The code only uses managesellers_id from the post data, no other post data.
  3. The row being updated is not being loaded. Doing $model->setId($id) will only set the data for the id, Magento will still think it's a "new" row and try to insert it, which may result in undesired behavior.

For "1": Without seeing the format of the DB table, it's hard to know this one. But based on the other code, it's treating managesellers_id like an ID for which it loads the data:

$id = Mage::app()->getRequest()->getParam('managesellers_id');
$model = Mage::getModel('managesellers/managesellers')->load($id);
$form->setValues($model->getData());

But the form field is:

$this->_addFieldsToFieldset($fieldset, array(
    'managesellers_id' => array(
        'label' => $this->__('ID'),
        'input' => 'text',
        'required' => false,
    ),
));

Unless the $model->getData() contains an entry for 'managesellers_id', then the form will not auto-populate the 'managesellers_id' field with data.

You could do this:

$id = Mage::app()->getRequest()->getParam('managesellers_id');
$model = Mage::getModel('managesellers/managesellers')->load($id);
$model->setManagesellersId($id);
$form->setValues($model->getData());

This will mean the form gets the correct data in the field.

I'd also recommend that this field is not editable (you may have already planned on that though :) ). If someone comes along and changes that, the code could end up updating another row in the database, wiping out another row's information.

It should either have a field type of hidden, or if you do need to display it set to readonly: 'readonly' => true,

For 2 & 3:

The saveAction should be tweaked so that it:

  • Uses the postdata
  • Loads the model so that Magento knows which row to update.

Something a bit more like this:

public function saveAction() 
{
    if($this->getRequest()->getParam('managesellers_id') > 0 ) 
    {
        try 
        {
            $id = $this->getRequest()->getParam('managesellers_id');
            $postData = $this->getRequest()->getPost();
            $model = Mage::getModel('managesellers/managesellers')->load($id);
            if ($model->isObjectNew() == false) {
                $model->setData($postData);
                $model->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved %s',$id));
            } else {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Could not find ID %s, no data updated',$id));
            }
        }
        catch (Exception $e) 
        {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            $this->_redirect('*/*/edit', array('managesellers_id' => $this->getRequest()->getParam('managesellers_id')));
        }
    }
    $this->_redirect('*/*/');
}
  • Didn't work.. getting an error.. It is adding a new row with new id and blank data. P.S- managesellers_id is an auto-increment field. – Rajat Agarwal Feb 6 '17 at 5:15
  • Solved the problem and updated the answer. Thanks for your help :) – Rajat Agarwal Feb 6 '17 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.