首页 > 解决方案 > 将数据从给定 ID 更新到具有相同 ID CodeIgniter 3 的另一个表

问题描述

我想创建从booking.booking_id获取的控制器端函数的给定$id参数更新client.client_status数据的函数

这是我的控制器

    function end($book_id = null) {
    if (!empty($book_id)) {
        // Booking Table
        $table = 'booking';
        $where = [
            'book_id' => $book_id,
        ];
        $data = [
            'room_status' => 1,
        ];
        $this->Model_Data->booking_update($table, $where, $data);
        
        // Client Table
        $table = 'client';
        $client_id = $???????; // How to get booking.client_id from given id parameters
        $where = [
            'client_id' => $client_id,
        ];
        $data = [
            'room_status' => 1,
        ];
        $this->Model_Data->booking_update($table, $where, $data);

        $this->session->set_flashdata('book_ended', 'Book Ended');
        redirect('book');
    }
    else {
        $this->session->set_flashdata('book_end_error', 'Book End Error');
        redirect('book');
    }
}

这是我的 SQL 表

在此处输入图像描述

标签: phpmysqlcodeignitercodeigniter-3

解决方案


根据您在 Question 中的评论,book_by_client_id等于client_idthen for :

访问单行:

(1) 作为对象的结果

$data_result = $this->db->get_where('booking',array('book_id'=>$book_id ))->row();

$book_by_client_id =$data_result->book_by_client_id;

$client_id = $book_by_client_id;     // client_id 

(2) 结果为数组

$data_result = $this->db->get_where('booking',array('book_id'=>$book_id ))->row_array();

$book_by_client_id =$data_result['book_by_client_id'];

$client_id = $book_by_client_id;     // client_id 

注意:有关row() 的更多信息;&& 行数组();

https://codeigniter.com/userguide3/database/results.html#result-rows


推荐阅读