Blog

Insert a User with Admin Privileges 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
{

    // After Admin is logged in insert user
    public function admin_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), $post_data['user_level']));
    }

}
?>

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

    public function insertuserintodb()
    {

        // 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('admin_user_add_errors', validation_errors());


            redirect(base_url() . 'users/new');
        }
        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("admin_user_add_success", "<p><strong>$first_name_post $last_name_post has been added to the database!</strong></p>");

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

            redirect(base_url() . 'users/new');

        }

    }

}
?>

View

<?php
// If the admin sessin variable DOESN'T exist
// redirect to home page
if(!$this->session->userdata('admin_session'))
{
    redirect(base_url());
}
?>

<div class="success">
    <?php echo $this->session->flashdata('update_success'); ?>
</div>

<form action="<?php echo base_url() . 'users/edit/process/' . $show_single_user['id']; ?>" method="post" class="side-by-side-form">
    <input type="hidden" name="user_details_update"/>
    <p>
        <label for="email">Email:</label><br />
        <input type="text" name="email" value="<?php echo $show_single_user['email']; ?>"/>
    </p>

    <p>
        <label for="first_name">First Name:</label><br />
        <input type="text" name="first_name" value="<?php echo $show_single_user['first_name']; ?>"/>
    </p>

    <p>
        <label for="first_name">Last Name:</label><br />
        <input type="text" name="last_name" value="<?php echo $show_single_user['last_name']; ?>"/>
    </p>

    <p>
        <select name="user_level">
            <?php
            // Assign the user level option
            // tags to display with the "Admin"
            // first ONLY if user record is
            // for an admin
            if($show_single_user['user_level'] == 'admin')
            {
                $first_user_level = 'admin';
                $second_user_level = 'normal';
            }
            else
            {
                $first_user_level = 'normal';
                $second_user_level = 'admin';
            }
            ?>
            <option value="<?php echo  $first_user_level;?>"><?php echo ucfirst($first_user_level); ?></option>
            <option value="<?php echo  $second_user_level;?>"><?php echo ucfirst($second_user_level); ?></option>
        </select>
    </p>

    <p>&nbsp;</p>
    <p>
        <input type="submit" value="Update"/>
    </p>
</form>

Leave a Reply