简体   繁体   中英

CodeIgniter getting the most recently entered row from database and using it in controller

I am new to codeigniter and am trying to figure out how to enter data into a table (phone) that is related to another row of data in another related table (client). Right now, I think what I need to do is get the most recently created client row's id and assign it as the foreign key field of the phone table's row. So my question I am asking is how do I get that so that I can echo it on the screen, so that I can be sure it can be used for other purposes. I am trying to store the value of the client_id into a variable $client_id. But I get the following php error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: client_id

Filename: views/form_success.php

Line Number: 1

For reference purposes I am including my code WITHOUT changing names since this is NOT a commercial project but is supposed to simulate one. MODEL

<?php

class insert_client extends CI_Model {
function __construct() {
    parent::__construct();
}

function form_insert($client_data) {
    // Insert Data into client table
    $this->db->insert('client', $client_data);
}

function get_id() {
    // Get id of most recently created record in client table.
    $this->db->select_max('client_id');
    $data = $this->db->get('client');
    return $data;
}
}

PART OF CONTROLLER (note the model is already loaded before the function in the code)

if ($this->form_validation->run() == FALSE) {
        $this->load->view('form_start');
        $this->load->view('client_form');
        $this->load->view('submit');
    } else {

        //CLIENT DATA
        $client_data = array (
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'address' => $this->input->post('address'),
            'apt_or_suite_number' => $this->input->post('apt_or_suite_number'),
            'zip_code' => $this->input->post('zip_code'),
            'state' => $this->input->post('state'),
            'email' => $this->input->post('email'),
        );          

        // Get ID
        $client_id = $this->insert_client->get_id();

        // Transfer to models and store in database
        $this->insert_client->form_insert($client_data);

        // Load Success View
        $this->load->view('form_success', $client_id);
    }

AND SUCCESS MESSAGE (form_success)

<div class="alert alert-success">Client created! User ID = <?php echo ($client_id); ?></div>

I think there is something wrong with getting the data:

function get_id() {
    // Get id of most recently created record in client table.
    $this->db->select_max('client_id');
    $query = $this->db->get('client');
    $data = $query->result();

/* or
    foreach ($query->result() as $row)
    {
        echo $row->title;
    }
*/

    return $data;
}

You can use the $this->db->insert_id() function.

Model:

function form_insert($client_data) {
    // Insert Data into client table
    $this->db->trans_start();
    $this->db->insert('client', $client_data);
    $insert_id = $this->db->insert_id();
    $this->db->trans_complete();
    return $insert_id;
}

Controller:

if ($this->form_validation->run() == FALSE) {
        $this->load->view('form_start');
        $this->load->view('client_form');
        $this->load->view('submit');
    } else {

        //CLIENT DATA
        $client_data = array (
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'address' => $this->input->post('address'),
            'apt_or_suite_number' => $this->input->post('apt_or_suite_number'),
            'zip_code' => $this->input->post('zip_code'),
            'state' => $this->input->post('state'),
            'email' => $this->input->post('email'),
        );          

        // Transfer to models and store in database and get last inserted id
        $client_id = $this->insert_client->form_insert($client_data);

        // Load Success View
        $this->load->view('form_success', $client_id);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM