2013-02-22 115 views
1

只是为了澄清所有答案在这里工作完美无瑕!自他首次发布以来,我选择了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; 

} 
+0

当然可以用相同的字段名称,你可以传递一个数组到模型。但你需要发布你的错误,并显示你的尝试。 – Wolf 2013-02-22 03:42:25

+0

编辑原始帖子 – RaGe10940 2013-02-22 03:49:27

回答

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'), 
     ); 
+0

仍然收到消息:未定义的变量:(什么变量)错误 – RaGe10940 2013-02-22 04:04:23

+0

@ RaGe10940你在模型或控制器中出现错误? – 2013-02-22 04:18:04

+0

错误在模型中发生 – RaGe10940 2013-02-22 04:34:13

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(); 
} 
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; 
     }