将数组传递给模型?

时间:2013-02-22 03:37:10

标签: php mysql codeigniter variable-assignment

只是为了澄清所有答案,这里工作完美无瑕!自从他首先发布以来,我选择了Edwins。

$this -> load -> model('staff_model');
            $this -> load -> library('encrypt');

            $arrData = array(
                'anum' => $this -> encrypt -> encode($this->input->post('anum')),
                'first' => $this -> input -> post('fname'),
                'last' => $this -> input -> post('lname'),
                'why' => $this -> input -> post('why'),
                'aidyear' => $this -> input -> post('aidyear'),
                'signintime' => NULL,
                'comments' => $this -> input -> post('comments'),
                );

            if ($insert = $this -> staff_model -> session($arrData)) {
                redirect('staff_controller/signin_info');
            } else {
                $this -> studentlogin();
            }

型号:

function session($arrData) {
    $null = NULL;
    $sql2 = "INSERT INTO session (anum, first, last, why, aidyear, signintime, studentcomments) 
             VALUES (?, ?, ?, ?, ?, ?, ?)";
    $insert = $this -> db -> query($sql2, $arrData);

    return $insert;

}

3 个答案:

答案 0 :(得分:2)

您不能像这样定义数组变量。尝试改为,

$data = array (
                    'encrypted' => $this->encrypt->encode('anum'),
                    'first' => $this->input->post('first'),
                    'last'  => $this->input->post('last'),
                    'aidyear' => $this->input->post('aidyear'),
                    'why' => $this->input->post('why'),
                    'comments' => $this->input->post('comments'),
       );

答案 1 :(得分:1)

控制器

$this -> load -> model('staff_model');
$this -> load -> library('encrypt');
$this->load->library('form_validation');
$this->load->helper('url');

//validate form input
        $this->form_validation->set_rules('anum', 'Anum', 'required');

        other validation rules
                .....

        if ($this->form_validation->run() == FALSE)
        {
                  redirect('back to previous page through controller');
        }else{


    $data = array (
                    'encrypted' => $this->encrypt->encode('anum'),
                    'first' => $this->input->post('first'),
                    'last'  => $this->input->post('last'),
                    'aidyear' => $this->input->post('aidyear'),
                    'why' => $this->input->post('why'),
                    'comments' => $this->input->post('comments'),
       );

     if ($insert = $this -> staff_model -> session($data)) {
         print_r($insert);
    }
}

模型

function session($data) {

        $query= $this->db->insert('session', $data); //insert into session table
            return $query->result();
} 

答案 2 :(得分:1)

在你的控制器中使用它:

$arrData = array();

//使用与数据库文件中的数据库表相同的名称

    $arrData['anum'] = $this->encrypt->encode('anum');
    $arrData['first'] = $this->input->post('first');
    $arrData['last'] = $this->input->post('last');
    $arrData['aidyear'] = $this->input->post('aidyear');
    $arrData['why'] = $this->input->post('why');
    $arrData['studentcomments'] = $this->input->post('comments');

  if ($this -> staff_model -> session($arrData)) {
                redirect('staff_controller/signin_info');
  }

在模型中使用

//不需要编写变量名,因为我们已在数组中使用相同的字段名

function session($arrData)
     {
       if($this->db->insert($arrData))
          return true;
        else
          return false;
      }