codeigniter中的唯一字段

时间:2015-05-30 08:43:57

标签: php codeigniter codeigniter-2 php-5.6

我需要在表单中创建唯一序列号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;
        // }
    // }
}

4 个答案:

答案 0 :(得分:1)

检查link

而不是user_name使用序列号。它适用于插入和更新。

答案 1 :(得分:1)

替换你的这一行,

$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]');

答案 2 :(得分:0)

  

如果为UNIQUE SN创建自己的函数,则使用三个东西

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

答案 3 :(得分:0)

要进行自定义验证,请在库文件夹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;
            }
        }
}
?>