为了账号安全,请及时绑定邮箱和手机立即绑定

在更新查询中获取 lastInsertId 的问题

在更新查询中获取 lastInsertId 的问题

PHP
SMILET 2021-08-27 09:24:06
我正在开发用于 oop php 中的属性/广告的 cms 以用于学习目的。我有三个与数据透视表相连的表。photos (name, extension, created_at, updated_at), property_photo (property_id, photo_id), properties (title, description, type_of_property, use_of_the_property, quadrature, location...) 当我尝试通过添加另一张照片来更新我的属性时,在我的数据透视表中 photo_id 从照片表中获取正确的 id,但我的 property_id 始终为 0。现在我知道 lastInsertId 函数仅在 INSERT 上不能用于 UPDATE 但我不知道如何以不同的方式获取 property_id。任何帮助表示赞赏。这是我在模型中的功能:广告模型:public function update_ad($data, $id){    $this->db->query('UPDATE properties SET title=:title, description=:description, type_of_property=:type_of_property, use_of_the_property=:use_of_the_property, quadrature=:quadrature, location=:location, price=:price, sales_clerk_info=:sales_clerk_info, booked=:booked, type_of_market=:type_of_market, type_of_payment=:type_of_payment WHERE id=:id');    $this->db->bind(':title', $data['title']);    $this->db->bind(':description', $data['description']);    $this->db->bind(':type_of_property', $data['type_of_property']);    $this->db->bind(':use_of_the_property', $data['use_of_the_property']);    $this->db->bind(':quadrature', $data['quadrature']);    $this->db->bind(':location', $data['location']);    $this->db->bind(':price', $data['price']);    $this->db->bind(':sales_clerk_info', $data['sales_clerk_info']);    $this->db->bind(':booked', $data['booked']);    $this->db->bind(':type_of_market', $data['type_of_market']);    $this->db->bind(':type_of_payment', $data['type_of_payment']);    $this->db->bind(':id', $id);    $this->db->execute();    $property_last_id = $this->db->lastId();    $dataimagecount = count($data['image']);    for ($i=0; $i < $dataimagecount ; $i++) {     $extension[$i] = explode('.',$data['image'][$i]);    $this->db->query('INSERT INTO photos (name, extension) VALUES (:name, :extension)');    $this->db->bind(':name', $extension[$i]['0']);    $this->db->bind(':extension', $extension[$i]['1']);    $this->db->execute();
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 152 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信