1

Just to clarify all the answers here work flawlessly! I chose Edwins since he posted first.

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

Model :

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;

}
2
  • Of course you can pass an array to your model. But you need to post your errors and show what you tried. Commented Feb 22, 2013 at 3:42
  • Edited the original post Commented Feb 22, 2013 at 3:49

3 Answers 3

2

You can't define array variables like this. Try to change to,

$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'),
       );
Sign up to request clarification or add additional context in comments.

3 Comments

Still get the Message: Undefined variable: (what ever variable) error
INSERT INTO session (anum, first, last, why, aidyear, signintime, studentcomments) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL) seems like nothing is passing because all of the values are NULL
@RaGe10940 you can't use $encrypted, $first etc.. directly. because it is in Array. Change $data['encrypted'], $data['first'] etc..
1

Controller

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

Model

function session($data) {

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

Comments

1

Use this in your controller:

$arrData = array();

//use same name as in your database table in array fileds like this

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

And in model just use

// No need to write variable names because we already use same field names in array

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.