2013-02-22 151 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; 
     }