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;

}
4

3 回答 3

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'),
       );
于 2013-02-22T04:01:27.353 回答
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();
} 
于 2013-02-22T05:46:19.267 回答
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;
      }
于 2013-02-22T06:22:54.027 回答