【问题标题】:CodeIgniter getting the most recently entered row from database and using it in controllerCodeIgniter 从数据库中获取最近输入的行并在控制器中使用它
【发布时间】:2014-11-26 22:25:58
【问题描述】:

我是 codeigniter 的新手,正在尝试弄清楚如何将数据输入到与另一个相关表(客户端)中的另一行数据相关的表(电话)中。现在,我认为我需要做的是获取最近创建的客户行的 id 并将其分配为电话表行的外键字段。所以我要问的问题是我如何得到它,以便我可以在屏幕上回显它,这样我就可以确定它可以用于其他目的。我正在尝试将 client_id 的值存储到变量 $client_id 中。但我收到以下 php 错误:

遇到了 PHP 错误

严重性:通知

消息:未定义变量:client_id

文件名:views/form_success.php

行号:1

出于参考目的,我在不更改名称的情况下包含我的代码,因为这不是一个商业项目,但应该模拟一个。 型号

<?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(注意模型在代码中的函数之前已经加载)

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

和成功消息(form_success)

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

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    您可以使用$this->db->insert_id() 函数。

    型号:

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

    控制器:

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

    【讨论】:

      【解决方案2】:

      我认为获取数据有问题:

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

      【讨论】:

      • 我不明白你为什么将标题放在回声中,也不明白为什么要在模型中回声。
      • 重点是,您需要使用 $query->result() 获取 $data。 $data 包含 $rows,每一行都有标题等字段,这只是 user_guide 中的示例。
      • 我明白了,如果我想在我的控制器中使用它怎么办?你看我尝试了你的解决方案,现在我正在处理数组到字符串的转换问题。
      • 别担心,我现在只是使用了行数。我想我并没有很好地解释我需要什么才能在这里被理解。但这是目前最简单的解决方案。
      猜你喜欢
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多