douchongbc72264 2017-11-02 10:22
浏览 46
已采纳

where子句中的列'id'对于3个表Codeigniter是不明确的

I am currently doing a system. In the system, there's a module called manage resident; where the manage resident will get the information of the resident if it is single, married and have a child(first,middle and last name for example).

I've done the adding or create new resident part, I am now in edit resident part. When I click the edit resident, I encountered error

Column 'id' in where clause is ambiguous

I have 3 tables: resident, married and children. Resident ID is the foreign key of married, while Married ID is the foreign key of children table.

My data example for each table(column Primary key, foreign key, first_name, middle_name, last_name

Resident -> id, Testing, Testing, Testing
Married  -> Married_id, resident_id, Testing, Testing, Testing
Children -> id, married_id, Testing, Testing, Testing

Question: How can I get the children of the selected Resident? Then display it separately in my fields in my view.

Controller

 $id = $this->uri->segment(4);
 $get_children = $this->Crud_model->get_children($id);

Model

public function get_children($id){
        $this->db->select('*');    
        $this->db->from('resident');
        $this->db->where('id',$id);
        $this->db->join('married', 'resident.id = married.resident_id');
        $this->db->join('children', 'resident.id = children.married_id');
        $query = $this->db->get();
        return $query->row();
    }

View

 <?php $explode = explode(",",$children->first_name); ?>

<div class="form-group">
  <label>First Name</label>
  <input type="text" class="form-control" name="child_fname" id="child_fname"><?= $explode?>
</div>
  • 写回答

2条回答 默认 最新

  • dongxi3859 2017-11-02 10:48
    关注

    Whenever you join tables with where condition you should write each column with its reference table name.

    so change

    $this->db->where('id',$id);
    

    to

    $this->db->where('resident.id',$id);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化