database getting updated wrong from custom module

2019-09-10 03:13发布

问题:

Hi I am working on a custom module form in Magento. I have used the default validation given by Magento.

Here is my form and passing form id to varienform

<form id="my-custom-form" action=""  method="post">
  <label> Form</label>

  <label>First Name</label>
  <strong>:</strong>
  <input class="input-text required-entry" type="text" name="fname" maxlength="20"><br>
  <label>Last Name</label>
  <strong>:</strong>
  <input class="input-text required-entry" type="text" name="lname" maxlength="20"><br>
  <label>Address</label>
  <strong>:</strong>
  <textarea class="required-entry" placeholder="Type your address" name="address"></textarea><br>
  <label>State</label>
  <strong>:</strong>
  <input class="required-entry" type="text" maxlength="20" name="state"><br>
  <label>City</label>
  <strong>:</strong>
  <input class="required-entry" type="text" maxlength="20" name="city"><br>
  <label>Mobile No</label>
  <strong>:</strong>
  <input class="required-entry" type="number" maxlength="10" name="mobileno">    <br>
  <input type="submit" name="submit" value="submit">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <input type="button" name="Cancel" value="cancel">
</form>

<script type="text/javascript">
  //< ![CDATA[
    var customForm = new VarienForm('my-custom-form');
  //]]>
</script>

I have added the db insert in indexcontroller

<?php
class MyCustom_Helloworld_IndexController extends     Mage_Core_Controller_Front_Action
{

  /*
  * this method privides default action.
  */
  public function indexAction()
  {
  $param = $this->getRequest()->getParams();

  $firstname = $param['fname'];
  $lastname = $param['lname'];
  $address = $param['address'];
  $state = $param['state'];
  $city = $param['city'];
  $mobile = $param['mobileno'];

  $model = Mage::getModel('helloworld/helloworld');
  // $model->setTitle($title);
  $model->setFirstname($firstname);
  $model->setLastname($lastname);
  $model->setAddress($address);
  $model->setState($state);
  $model->setCity($city); 
  $model->setMobileno($mobile); 
  $model->save();   

  /*
  * Initialization of Mage_Core_Model_Layout model
  */
  $this->loadLayout();
  /*
  * Building page according to layout confuration
  */
  $this->renderLayout();

  }
}

When I run my module the form appears and when I add no values and click submit the validation errors are getting displayed so no problem with validation but when I checked into db the tables are getting updated with null values. What could be the reason ?

回答1:

You can try following ways to debug :

1) Add action to your form and do echo exit in controller, this can confirm if data is reaching in your controller.

2) Once controller is reachable, print_r the $param. Also you should always add server side validation which be as follows :

if(in_array('', $param)){ return $this->_redirect('your handler');}

3) Once data is reaching with proper key value pairs in your controller using above step 2, check if your model is properly initianized in model folder.

4) Check if your columns names are properly written in set method.

5) Last but might be the main problem, clear all the magento caches from backend and try saving data again.



回答2:

this will prevent you from adding null value.

if($this->getRequest()->getParams()) {
    $param = $this->getRequest()->getParams();


    $firstname = $param['fname'];
    $lastname = $param['lname'];
    $address = $param['address'];
    $state = $param['state'];
    $city = $param['city'];
    $mobile = $param['mobileno'];



    $model = Mage::getModel('helloworld/helloworld');
    // $model->setTitle($title);
    $model->setFirstname($firstname);
    $model->setLastname($lastname);
    $model->setAddress($address);
    $model->setState($state);
    $model->setCity($city); 
    $model->setMobileno($mobile); 
    $model->save(); 
    $this->_redirectReferer();
    }else {
      /*
    * Initialization of Mage_Core_Model_Layout model
    */
    $this->loadLayout();
    /*
    * Building page according to layout confuration
    */
    $this->renderLayout();
    }