Blog

Insert a Submitted User into a Database Using CodeIgniter with Model, Controller, and View Code Examples
Posted on July 2, 2015 in CodeIgniter, MVC, MySQL, PHP by Matt Jennings

Model

<?php
class UserDashboardModel extends CI_Model
{

    // Registration form insert user
    public function insert_user($post_data)
    {

        $this->db->query("INSERT INTO users (first_name, last_name, email, password, user_level, created_at, updated_at) VALUES (?,?,?,?,?,NOW(),NOW())", array($post_data['first_name'], $post_data['last_name'], $post_data['email'], substr(md5($post_data['password']), 0, -2), 'normal'));

    }

}
?>

Controller

<?php
class UserDashboard extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();

        // Load the UserDashboardModel on all pages
        $this->load->model('UserDashboardModel');

        $this->output->enable_profiler();
    }

    // Register a new user with a "user_level" of normal
    // and use a select tag
    public function registernewuser()
    {
        // Load the form helper
        $this->load->helper('form');

        // Load the form_validation library
        $this->load->library('form_validation');

        // Set form validation rules
        $this->form_validation->set_rules('email', '<strong><em>email</em></strong>', 'required|xss_clean|trim|min_length[5]|max_length[100]|is_unique[users.email]|valid_email');

        $this->form_validation->set_rules('first_name', '<strong><em>first name</em></strong>', 'required|xss_clean|trim|min_length[2]|max_length[100]');

        $this->form_validation->set_rules('last_name', '<strong><em>last name</em></strong>', 'required|xss_clean|trim|min_length[2]|max_length[100]');

        $this->form_validation->set_rules('password', '<strong><em>password</em></strong>', 'required|xss_clean|trim|min_length[6]|max_length[20]');

        $this->form_validation->set_rules('confirm_password', '<strong><em>password confirmation</em></strong>', 'required|xss_clean|trim|matches[password]');

        // If there are form validation errors
        if($this->form_validation->run() == FALSE)
        {

            // Set registration errors as a
            // flash data variable
            $this->session->set_flashdata('register_errors', validation_errors());


            redirect(base_url() . 'register');
        }
        else
        {

            // Set form post first_name and last_name
            // as variables
            $first_name_post = $this->input->post('first_name', TRUE);
            $last_name_post = $this->input->post('last_name');

            // Set registration success message
            // as flash data
            $this->session->set_flashdata("register_success", "<p><strong>$first_name_post $last_name_post, thank you for registering!</strong></p>");

            // Run model to insert form fields into
            // database
            $this->UserDashboardModel->insert_user($this->input->post(NULL, TRUE));

            redirect(base_url() . 'register');

        }

    }

}
?>

View

<?php
// Load the form helper
$this->load->helper('form');
// Load the form_validation library
$this->load->library('form_validation');
?>

<?php
// Display validation errors if they exist
echo $this->session->flashdata('register_errors');
?>

<?php
// Display success message if it exists
echo $this->session->flashdata('register_success');
?>

<form id="signin-register-add" action="<?php echo base_url() . 'register/newuser'; ?>" method="post">
    <input type="hidden" name="register"/>
    <div class="row-fluid">
        <div class="col-md-3"><p><label for="email">Email Address:</label></p></div>
        <div class="col-md-9"><p><input type="text" name="email" value="<?php echo set_value('email'); ?>"/></p></div>
    </div>

    <div class="row-fluid">
        <div class="col-md-3"><p><label for="first_name">First Name:</label></p></div>
        <div class="col-md-9"><p><input type="text" name="first_name" value="<?php echo set_value('first_name'); ?>"/></p></div>
    </div>

    <div class="row-fluid">
        <div class="col-md-3"><p><label for="last_name">Last Name:</label></p></div>
        <div class="col-md-9"><p><input type="text" name="last_name" value="<?php echo set_value('last_name'); ?>"/></p></div>
    </div>

    <div class="row-fluid">
        <div class="col-md-3"><p><label for="password">Password:</label></p></div>
        <div class="col-md-9"><p><input type="password" name="password"/></p></div>
    </div>

    <div class="row-fluid">
        <div class="col-md-3"><p><label for="confirm_password">Confirm Password:</label></p></div>
        <div class="col-md-9"><p><input type="password" name="confirm_password"/></p></div>
    </div>

    <div class="row-fluid">
        <div class="col-md-9 col-md-offset-3"><p><input type="submit" value="Register"/></p></div>
    </div>
</form>

Leave a Reply