在数据库列中插入外键值时更新查询的问题

时间:2019-06-19 12:51:33

标签: php sql pdo

我正在研究oop php中的属性的cms,以进行学习。我有一个包含每个属性的图片的画廊。现在,我正在尝试使这些图像之一成为该特定属性的主要图像。我有三张桌子。

properties (id, location, price, main_photo_id)

property_photo (id, property_id, photo_id)

photos (id, name, extension)

我在编写查询时遇到了麻烦,该查询将使用photos表中的id(或property_photo表中的photo_id)更新main_photo_id列(属性表)。我知道查询效果不好,因为它总是在main_photo_id列中写入0,但是我在sql中相对缺乏经验,似乎找不到问题。任何帮助表示赞赏。

AdModel:

public function MainPhotoInsert($photoid)
{   
    $this->db->query('UPDATE properties INNER JOIN photos ON photos.id = properties.id SET properties.main_photo_id = :main_photo_id');     
    $this->db->bind(':main_photo_id', $photoid);
    $this->db->execute();
}

AdsController:

public function galleryAction()
 {
    if (!isset($_GET['id'])) {
        $photo_id = $_SESSION['photo_id'];
    } else {
        $photo_id = $_GET['id'];
    }


    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); 

        if(isset($_POST['radio']))
        {
            $this->AdModel->MainPhotoInsert($photoid);
            redirect('ads/index');
        }   
    }

    $data = $this->AdModel->getPhotosForProperty($photo_id);
    $data1 = $this->AdModel->MainPhotoData($photo_id);

    $this->view->render('ads/gallery', $data, $data1);
 }

gallery.php

<form action="/ads/gallery?id=<?php echo $_GET['id']; ?>" method="POST">
  <?php foreach ($data as $key => $value) : ?>   
        <img src="<?php echo '/public/photos/'.$value->name.'.'.$value->extension ?>" class="img-fluid img-thumbnail" width="250" height="250">
    <input type="radio" name="radio" value="<?php echo $value->photo_id; ?>" >Make main
    <br>
  <?php endforeach; ?>
    <br>
    <br>
    <button type="submit" name="submit" value="submit" class="btn btn-success form-control">Submit</button>
</form>

1 个答案:

答案 0 :(得分:1)

我建议使用此查询:

UPDATE properties SET main_photo_id = :main_photo_id WHERE id = :property_id;

但这要求您知道属性ID。现在,我看到您在property_photo表中具有属性ID,因此您也可以使用它:

public function MainPhotoInsert($photoid)
{   
    $subQuery = "SELECT property_id 
                 FROM property_photo 
                 WHERE id = :main_photo_id2";
    $this->db->query("UPDATE properties 
                      SET main_photo_id = :main_photo_id1
                      WHERE id = ($subQuery)");     
    $this->db->bind(':main_photo_id1', $photoid);
    $this->db->bind(':main_photo_id2', $photoid);
    $this->db->execute();
}

我正在使用子查询来查找属性的ID,因此我需要将照片ID绑定两次。

注意:我不确定,但是Mysql可能会由于使用两个不同表中的id而感到困惑。如果出现错误,请使用如下别名:

public function MainPhotoInsert($photoid)
{   
    $subQuery = "SELECT B.property_id 
                 FROM property_photo AS B 
                 WHERE B.id = :main_photo_id2";
    $this->db->query("UPDATE properties AS A
                      SET A.main_photo_id = :main_photo_id1
                      WHERE A.id = ($subQuery)");     
    $this->db->bind(':main_photo_id1', $photoid);
    $this->db->bind(':main_photo_id2', $photoid);
    $this->db->execute();
}

所有代码都未经测试。