Phantom Moore
Phantom Moore

Reputation: 41

Add user on Codeigniter 3

Hi i am new here and i dont know how to use codeigniter and now im confused. So i am currently trying to add user data to the database using codeigniter 3.1.10 . When i click the " save " button there's nothing to display. The page was refresh

Can you help me please? Models:

    function add_user($data) {
    $this->db->set("username",$data["username"]);
    $this->db->set("password",$data["password"]);
    $this->db->set("indirizzo",$data["indirizzo"]);
    $this->db->set("citta",$data["citta"]);
    $this->db->set("cap",$data["cap"]);
    $this->db->insert("user");
    $ins_id =$this->db->insert_id();
    return $ins_id;
}

Controllers:

    function add() {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('save', '', 'trim|required|number');
    if ($this->form_validation->run()) :
        $data = array(
            "username"=>$this->input->post("username"),
            "password"=>$this->input->post("password"),
            "indirizzo"=>$this->input->post("indirizzo"),
            "citta"=>$this->input->post("citta"),
            "cap"=>$this->input->post("cap"),
        );

        $user_id= $this->user_model->add_user($data);
        $this->log_model->scrivi_log($user_id,"user","add");
    $this->session->set_flashdata('feedback', 'User added.');
        redirect("user/pageuser/".$user_id);
    else :  
        $content = $this->view->load("content");
        $content->load("clienti_form","user/add");
        $this->view->render();
    endif;
}

Upvotes: 2

Views: 385

Answers (2)

Mohd Qayyoom Khan
Mohd Qayyoom Khan

Reputation: 78

function add_user($data) {
  $this->db->insert("user",$data);
  $ins_id =$this->db->insert_id();
  return $ins_id;
  }

use this in model..

and in controller set rules for each like this

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
// for all other

Upvotes: 0

mindmaster
mindmaster

Reputation: 1888

Your doing a lot wrong, starting from the fact that your doing stuff from the model in your controller, and you should divide it, otherwise your not using the concept of MVC.

Try something like this, being hard to help you, without seeing the whole code:

Model

function add_user() 
{
    $data = array(
        'username' => $this->input->post('username'),
    'password' => $this->input->post('password'),
    'indirizzo' => $this->input->post('indirizzo'),
    'citta' => $this->input->post('citta'),
    'cap' => $this->input->post('cap')
    );

    return $this->db->insert('user', $data);
}

Controller

function add() {
    $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
    $this->form_validation->set_rules('indirizzo', 'Indirizzo', 'required');
    $this->form_validation->set_rules('citta', 'Citta', 'required');
    $this->form_validation->set_rules('cap', 'Cap', 'required');

    $errore = true;

    if ($this->form_validation->run() === FALSE){ // if doesnt work load your view

            $this->load->view('your view');

    }
    else { 

    $this->user_model->add_user();
        $this->log_model->scrivi_log($user_id,"user","add");
        $this->session->set_flashdata('feedback', 'User added.');
        redirect("user/pageuser/".$user_id);

        $content = $this->view->load("content");
        $content->load("clienti_form","user/add");
        $this->view->render();
    }
}

You really should try and search more about it, and learn! I could learn a lot of the basics of CodeIgniter, watching this channel that has great content, and explains every detail: https://www.youtube.com/playlist?list=PLillGF-RfqbaP_71rOyChhjeK1swokUIS

Upvotes: 1

Related Questions