CODEIGNITER4:如何 link 值从 table_1 到 table_2 我使用 SELECT OPTION 标签插入

CODEIGNITER4: how to link value from table_1 to table_2 that I insert using SELECT OPTION tag

看到我有 2 tables tbl_officetbl_result

| id | office_name |   | id | office      | rating    | comment |
|----|-------------|   |----|-------------|-----------|---------|
| 1  | Records     |   | 1  | Records     | Satisfied |         |
| 2  | Logistics   |   | 2  | Logistics   | Satisfied |         |
| 3  | HR          |   | 3  | HR          | Neutral   |         |

我在表单上使用了 SELECT OPTION 标签来获取 office_name 的行值tbl_office

控制器

namespace App\Controllers;
use App\Models\SurveyModel;
use App\Models\OfficeMOdel;

class Survey extends BaseController
{
    public function index()
    {
        $data = [];
        helper(['form','url']);

        $office = new OfficeMOdel();
        $data['office'] = $office->findAll();
       

        if($this->request->getMethod() == 'post'){

            $rules = [
                'office' => ['label' => 'Office', 'rules' => 'required'],
                'rating' => ['label' => 'Rating', 'rules' => 'required']
            ];

        if (!$this->validate($rules)){
            $data['validation'] = $this->validator;
        }else{
            $model = new SurveyModel();

            $newData = [
                'office' => $this->request->getVar('office'),
                'rating' => $this->request->getVar('rating'),
                'comment' => $this->request->getVar('comment'),
            ];

            $model->save($newData);
            $session = session();
            $session->setFlashdata('success','Your Feedback has been successfully added to our system!',);

            return redirect()->to('survey/confirmation');
         }
        }

        echo view ('templates/header_form', $data);
        echo view ('surveyform');
        echo view ('templates/footer_form');
    }

PHP/HTML 用于显示 office_name

的值
<select name="office">

   <?php foreach($office as $row) :?>
   <option><?php echo $row['office_name'] ?></option>
   <?php endforeach; ?>

</select>

它工作,但是当我从 office_name 更新值 tbl_officetbl_result上的值不变。有没有办法 link 这些值,而不是在我将它插入另一个 table 时只获取选项的值?

非常感谢您的回答。初学者。

您不应在 tbl_result 中存储 office 名称。相反,您应该存储 office_id:

CREATE TABLE `tbl_office` (
    id int primary key auto_increment,
    name varchar(64)
);

CREATE TABLE `tbl_rating` (
    id int primary key auto_increment,
    name varchar(64)
);

CREATE TABLE `tbl_result` (
    id int primary key auto_increment,
    office_id int,
    rating_id int,
    comment text
);

SELECT `res`.`id`, `o`.`name` AS `office`, `r`.`name` AS `rating`, `comment`
FROM `tbl_result` `res`
JOIN `tbl_office` `o` ON `o`.`id` = `res`.`office_id`
JOIN `tbl_rating` `r` ON `r`.`id` = `res`.`rating_id`

Share SQL fiddle

+====+===========+===========+=========+
| id | office    | rating    | comment |
+====+===========+===========+=========+
| 1  | Records   | Satisfied | (null)  |
+----+-----------+-----------+---------+
| 2  | Logistics | Satisfied | (null)  |
+----+-----------+-----------+---------+
| 3  | HR        | Neutral   | (null)  |
+----+-----------+-----------+---------+

在索引

中加入两个table
  $result = $this->model->select('request_post.*')
            ->join('users', 'request_reply.user_id = users.id', 'left')
            ->join('request_post', 'request_reply.post_id = request_post.id', 'left')
             ->where(['request_post.id'=>['1']])
          
            ->paginate(10, 'default',1, 0);