Blog

Update a Single MySQL Table Row 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
{

    // Update a single database row
    public function update_single_record($post_data, $user_id)
    {
        $this->db->query("UPDATE users SET first_name = '{$post_data['first_name']}', last_name = '{$post_data['last_name']}', email = '{$post_data['email']}', user_level = '{$post_data['user_level']}', updated_at = NOW() WHERE id = $user_id");

    }

}
?>

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 executeupdatesingleuser($user_id)
    {

        // Set user first/last name as variable
        $update_success = '<p><strong>The profile of ' . $this->input->post('first_name', TRUE) . ' ' . $this->input->post('last_name', TRUE) . ' has been successfully updated.</strong></p>';

        // Create a success message flashdata session
        $this->session->set_flashdata('update_success', $update_success);

        $this->UserDashboardModel->update_single_record($this->input->post(NULL, TRUE), $user_id);


        redirect(base_url() . 'users/edit/' . $user_id);

    }
}
?>

View

<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