【问题标题】:Data inserted into a database when refresh page in CodeIgniter在 CodeIgniter 中刷新页面时插入数据库的数据
【发布时间】:2015-05-22 19:15:27
【问题描述】:

表单缓存我的输入并在刷新页面时将其插入如何停止它 我应该使用 CodeIgniter 验证还是可以使用我的验证?

查看:

 <html>
 <head></head>
</body>
<?php echo validation_errors(); ?>  
 <?php echo form_open('speed/insert_to_db'); ?>
 Branch: <input type="text" name="branch" /><br/>
Business Unit: <input type="text" name="buinessUnit" /><br/>
Device Type: <input type="text" name="deviceType" /><br/>
Brand: <input type="text" name="brand" /><br/>
Device Model: <input type="text" name="deviceModel" /><br/>
SN: <input type="text" name="SN" /><br/>
status: <input type="text" name="status" /><br/>
department: <input type="text" name="department" /><br/>
username: <input type="text" name="username" /><br/>
notes: <input type="textarea" name="notes" /><br/>
computername: <input type="text" name="computerName" /><br/>
Save:<input type="submit" name="save" />

</form>

控制器:

class Speed extends CI_Controller {

        function insert_to_db()
           {
               //$this->output->enable_profiler(TRUE);
             $this->load->model('add_model');
             $this->add_model->insert_into_db();
             $this->load->view('pages/home');//loading success view
           }


}

模型:将数据插入数据库

<?php
class add_model extends CI_Model {

       public function insert_into_db(){
           $post=$this->input->post();
           if(!isset($post['save'])) return;
           $data=array('Branch'=>$post['branch'],'BusinessUnit'=>$post['buinessUnit'],'DeviceType'=>$post['deviceType'],'Brand'=>$post['brand'],'DeviceModel'=>$post['deviceModel'],'SN'=>$post['SN'],'Status'=>$post['status'],'Departmant'=>$post['department'],'UserName'=>$post['username'],'Notes'=>$post['notes'],'ComputerName'=>$post['computerName']);
           $this->db->insert('hardware_assets', $data);
return $this->db->insert_id(); // if using mysql
       }
}

【问题讨论】:

    标签: php codeigniter codeigniter-2


    【解决方案1】:

    您可以为控制器生成一个随机数,如下面的代码,

    $rand = rand( 100000, 999999);
    $data['rand'] = $rand;
    $this->session->set_userdata('form_hash',$rand);
    

    在第一次控制器调用时,将其传递给隐藏变量中的表单,就像在视图中一样,

    <input type="hidden" name="form_hash" value="<?php echo $rand?>" />
    

    因此,每次调用控制器时,您都会比较数字是否相同,如果验证通过,则继续,如果不通过,则不要插入数据。

    if($this->input->post('form_hash') == $this->session->userdata('form_hash'))
    {
        // process with database entry
    }
    

    请注意,这两个哈希只会匹配一次,第一次提交表单时,每隔一次,if条件都会失败。

    所以,你的最终代码会是这样的,

    class Speed extends CI_Controller
    {
        function insert_to_db()
        {
            $this->load->model('add_model');
    
            if($this->input->post('form_hash') == $this->session->userdata('form_hash'))
            {
                $this->add_model->insert_into_db();
            }
            $rand = rand( 100000, 999999);
            $data['rand'] = $rand;
            $this->session->set_userdata('form_hash',$rand);
    
            //loading success view
            $this->load->view('pages/home', $data);
        }
    }
    

    别忘了在表单的视图中插入这一行,

    <input type="hidden" name="form_hash" value="<?php echo $rand?>" />
    

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2016-12-01
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      相关资源
      最近更新 更多