【问题标题】:Unique field in codeignitercodeigniter 中的唯一字段
【发布时间】:2015-08-13 04:16:34
【问题描述】:

我需要在我的表单中创建唯一序列号 SN。我试过这样做,但我不明白这个功能是如何工作的。请解释清楚,因为我是使用 codeigniter 编程的新手

视图:我的视图(表单)

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/>  //that i need unique
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>
</body>
</html>

模型:将数据插入数据库

<?php
class add_model extends CI_Model {

       public function insert_into_db(){
           $post=$this->input->post(); 
           //insert data with query builder
           $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

class Speed extends CI_Controller {

        function insert_to_db()
           {
               $this->load->helper(array('form', 'url'));
              $this->load->library('form_validation');
               $this->form_validation->set_rules('branch', 'Branch', 'trim|required|xss_clean');
               $this->form_validation->set_rules('buinessUnit', 'BuinessUnit', 'trim|required|xss_clean');
               $this->form_validation->set_rules('deviceType', 'DeviceType', 'trim|required|xss_clean');
               $this->form_validation->set_rules('brand', 'Brand', 'trim|required|xss_clean');
               $this->form_validation->set_rules('deviceModel', 'DeviceModel', 'trim|required|xss_clean');
               $this->form_validation->set_rules('SN', 'SN', 'trim|required|xss_clean');
               $this->form_validation->set_rules('status', 'Status', 'trim|required|xss_clean');
               $this->form_validation->set_rules('department', 'Department', 'trim|required|xss_clean');
               $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
               $this->form_validation->set_rules('notes', 'Notes', 'trim|required|xss_clean');
               $this->form_validation->set_rules('computerName', 'ComputerName', 'trim|required|xss_clean');
            /*   

           */
           if ($this->form_validation->run() == FALSE)
             {
                $this->load->view('pages/home');
             }
           else
             {
                 $this->load->model('add_model');
             $this->add_model->insert_into_db();
             $this->load->view('pages/home');//loading success view
            //$this->load->view('pages/formsuccess');
             }
           }

    // public function username_check($str)
    // {
        // if ($str == 'test')
        // {
            // $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            // return FALSE;
        // }
        // else
        // {
            // return TRUE;
        // }
    // }
}

【问题讨论】:

  • 设置要存储SN字段值的唯一数据库列
  • cogeigniter 3 支持规则 is_unique[tablename.field]
  • 使用序列号做什么?用于唯一记录存储,然后根据 id 检索它?如果是这样,你可以忽略(SN)的Column,让db帮你生成一个。

标签: php codeigniter codeigniter-2 php-5.6


【解决方案1】:

检查link

使用序列号代替用户名。它适用于插入和更新。

【讨论】:

  • 感谢您的帮助,如果您知道刷新页面时如何停止插入数据(重复)
  • 当我重新加载页面时,它会提交最后一次插入数据库的数据
【解决方案2】:

替换你的这一行,

$this->form_validation->set_rules('SN', 'SN', 'trim|required|xss_clean');

与,

$this->form_validation->set_rules('SN', 'SN', 'trim|required|xss_clean|is_unique[hardware_assets.SN]');

【讨论】:

  • 但是用这个解决方案我怎么能改变消息
【解决方案3】:

如果您为 UNIQUE SN 创建自己的函数,则使用三件事

$date = date("ymdhis");
 $SD = rand(10,2000);
$ID =$this->db->insert_id();
$SN = $ID.SD.$date; 

【讨论】:

    【解决方案4】:

    要进行自定义验证,请在您的库文件夹 my_form_validation.php 中添加一个文件,因为您可以根据需要修改此函数或创建新函数,

        <?php
    
        class MY_Form_validation extends CI_Form_validation {
            //used to check unique value at update record
            function unique($value, $params) {
                $CI = & get_instance();
                $CI->load->database();
                $CI->form_validation->set_message('unique', 'The %s is already taken.');
                list($table, $field, $id_field, $id) = explode(".", $params, 4);
    
                $data = $CI->db->select($field)->from($table)
                                ->where($field, $value)
                                ->where($id_field . " != ", $id)
                                ->get()->num_rows();
                if ($data) {
                    return FALSE;
                } else {
                    return TRUE;
                }
            }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      相关资源
      最近更新 更多