Jump to content

Module is inserting blank value to the database when the page is refreshed


prestashop_newuser

Recommended Posts

Hi,
Currently I am doing a module from scratch. My codes for module looking like this

<?php
/*
*
*/
if(!defined('_PS_VERSION_'))
  exit;


class MyModule extends Module {
  /** @var max image size */
  protected $maxImageSize = 307200;


  public function __construct() {
    $this->name = 'mymodule';
    $this->tab = 'front_office_features';
    $this->version = '0.0.1';
    $this->need_instance = 0;
    
    parent::__construct();


    $this->displayName = $this->l('Mymodule');
    $this->description = $this->l('Mymodule.');
    $this->confirmUninstall = $this->l('Are you sure to uninstall Mymodule?');
  }


  public function install() {
    if (!parent::install() || !$this->installDB() || !$this->registerHook('home') || !$this->registerHook('header'))
      return false;
    return true;
  }
  
  public function installDB(){
    if (!Db::getInstance()->Execute('
    CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'storedetails` (
    `store_id` int(10) unsigned NOT NULL auto_increment,
    `store_name` varchar(255) NOT NULL,
    `phone_num` int(15) NOT NULL,
    `description` varchar(255) NOT NULL,
        PRIMARY KEY (`store_id`))
    ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'))
    return false;
    return true;
  }
  
  
  public function uninstall() {
    if (!parent::uninstall() || !$this->uninstallDB())
      return false;
    return true;
  }


  
  public function getContent() {
    $store_name=Tools::getValue('store_name');
    $phone_number=Tools::getValue('phone_number');
    $desc = Tools::getValue('desc');
    
    if (Tools::isSubmit('submit_details')) {
      if(Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'storedetails` (`store_id`, `store_name`, `phone_num`,`description`)
      VALUES ("","'.$store_name.'","'.$phone_number.'","'.$desc.'")')) {
      $this->_html .=$this->displayConfirmation($this->l('Settings updated successfully'));
      } else {
        $this->_html .= $this->displayError($this->l('You Have Some Errors'));
      }
    } 
     
   $this->_displayForm();
    return $this->_html;
  }
  
  private function _displayForm() {


   $this->_html .=  '
    <link rel="stylesheet" href="../modules/mymodule/css/styles.css" />
    <script src="../modules/mymodule/js/upload.js" type="text/javascript" charset="utf-8"></script>
   ';
    $this->_html .= '
      <div id="admin-wrapper">
        <div class="store-info-wrap">
          <form id="formID" method="post" action="'.$_SERVER['REQUEST_URI'].'" enctype="multipart/form-data">
            <div id="form-content">
              <div>
              <label for="storename">'.$this->l('Store Name').'</label>
                <input class=" text-input" type="text"  name="store_name" id="store_name" value="" />
              <br/>
              </div>
              <div>
            <label>'.$this->l('Phone Number').'</label>
            <input class="text-input" type="text"  name="phone_number" id="phone_number" value=""/>
          </div>
            <div>
            <label>'.$this->l('Description').'</label>
             <textarea class="text-input" name="desc" id="desc" rows="8" cols="52" ></textarea>
          </div>


          
          <div style="margin-top:10px; text-align: center;">
            <input type="submit" name="submit_details" value="'.$this->l('submit_details').'" class="button" />
            </div>          
            </div><!--#form-content-->
             
          </form>
        </div><!--.store-info-wrap-->
        
      </div><!--#admin-wrapper-->
       ';


  }


  public function hookHome($params) {
  
  }
  
  public function hookHeader() {
    $this->context->controller->addCSS(($this->_path).'css/store_styles.css', 'all');
    
  }


}

Now here the module is doing fine. It is inserting the values to the database. But there is one problem that when I am doing refresh the page it is inserting the last inserted value again. I know that to avoid this I need to redirect to another page. But I have only this page to show in my module. I ha also tried jQuery Ajax validation but that one is doing validation part when datas are inserting but here when I am doing refresh the page it is inserting the last value. So can someone kindly tell me  how to solve this? Any help and suggestions will be really appreciable. Thanks

 

 

Link to comment
Share on other sites

So before inserting teh data in DB  why don't you initially chked if the data is present in db or not like below

 

if (Tools::isSubmit('submit_details')) {
 
  // ---- check here if teh record in storedetails exists with store_name and $phone_number and if not present then only do following

  if(Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'storedetails` (`store_id`, `store_name`, `phone_num`,`description`) VALUES  ("","'.$store_name.'","'.$phone_number.'","'.$desc.'")')) {
    $this->_html .=$this->displayConfirmation($this->l('Settings updated successfully'));
  } else {
    $this->_html .= $this->displayError($this->l('You Have Some Errors'));
  }
}

 

Link to comment
Share on other sites

did you placed teh code  to chk redord is present in db or not insted of
 

    // ---- check here if teh record in storedetails exists with store_name and $phone_number and if not present then only do following

 


like this
 

    $insert = false;
    $sql = 'select * `'._DB_PREFIX_.'storedetails` where `store_name` = "'.$store_name.'" and `phone_num` = "'.$phone_number.'"';
    $result = Db::getInstance()->getRow($sql);
    if($result == 0) {
      $insert = true;
    }


    if (Tools::isSubmit('submit_details') && $insert == true) {
      if(Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'storedetails` (`store_id`, `store_name`, `phone_num`,`description`)
      VALUES ("","'.$store_name.'","'.$phone_number.'","'.$desc.'")')) {
      $this->_html .=$this->displayConfirmation($this->l('Settings updated successfully'));
      } else {
        $this->_html .= $this->displayError($this->l('You Have Some Errors'));
      }
    }

 

 

Link to comment
Share on other sites

  • 1 year later...

Hi,

 

I am new in prestashop. I am trying to develop a simple module.

 

Two text box on front end 1.Name, 2. Phone no.

Two value will inserted in database.

 

I have't get value from front end tpl file. And my module was working fine through back end panel.

 

My mymodule.php file like this

<?php
/*
*
*/
if(!defined('_PS_VERSION_'))
  exit;


class MyModule extends Module {
  /** @var max image size */
  protected $maxImageSize = 307200;


  public function __construct() {
    $this->name = 'mymodule';
    $this->tab = 'front_office_features';
    $this->version = '0.0.1';
    $this->need_instance = 0;
    
    parent::__construct();


    $this->displayName = $this->l('Mymodule');
    $this->description = $this->l('Mymodule.');
    $this->confirmUninstall = $this->l('Are you sure to uninstall Mymodule?');
  }


  public function install() {
    if (!parent::install() || !$this->installDB() || !$this->registerHook('home') || !$this->registerHook('header'))
      return false;
    return true;
  }
  
  public function installDB(){
    if (!Db::getInstance()->Execute('
    CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'storewhatapps` (
    `store_id` int(10) unsigned NOT NULL auto_increment,
    `name` varchar(255) NOT NULL,
    `phone_num` int(15) NOT NULL,
    
        PRIMARY KEY (`store_id`))
    ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'))
    return false;
    return true;
  }
  
  
  public function uninstall() {
    if (!parent::uninstall() || !$this->uninstallDB())
      return false;
    return true;
  }


  
  public function getContent() {
  
    $name=Tools::getValue('name');
    $phone_number=Tools::getValue('phone_number');
    if (Tools::isSubmit('submit_details')) {
      if(Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'storewhatapps` (`store_id`, `name`, `phone_num`)
      VALUES ("","'.$name.'","'.$phone_number.'")')) {
      $this->_html .=$this->displayConfirmation($this->l('Settings updated successfully'));
      } else {
        $this->_html .= $this->displayError($this->l('You Have Some Errors'));
      }
    } 
     
   //$this->_displayForm();
   $this->display(__FILE__, 'mymodule.tpl');
    //return $this->_html;
	
	return $this->display(__FILE__, 'mymodule.tpl');
  }
  
  

  public function hookHome($params) {
   
  return $this->display(__FILE__, 'mymodule.tpl');
         
  }
  
  public function hookHeader() {
    $this->context->controller->addCSS(($this->_path).'css/store_styles.css', 'all');
     
  }


}

And my mymodule.tpl file look like this

 

    <fieldset>
	
	 <form id="formID" method="post" action="#">
		<p>
			<label for="name">Name:</label>
			<input id="name" name="name" type="text"/>
		</p>
		<p>
			<label for="phone_number">Phone Number:</label>
			<input id="phone_number" name="phone_number" type="text" />
		</p>
		<p>
			<label> </label>
			  <input type="submit" name="submit_details" class="button" />
		</p>
	</form>
</fieldset>

 

Please guide me

Link to comment
Share on other sites

Hi,

 

I am new in prestashop. I am trying to develop a simple module.

 

Two text box on front end 1.Name, 2. Phone no.

Two value will inserted in database.

 

I have't get value from front end tpl file. And my module was working fine through back end panel.

 

M

 

Please guide me

 

 

please open new post in development section.  refrain when  possible of posting your 'similar' question in existing/old topic for best results.

 

this post closed.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...