从外键表中获取第一张图像

时间:2019-04-16 16:13:17

标签: php html mysql

我已经在以下线程的帮助下创建了一个发布系统:Add a product with details (name,quantity,multiple images) and retrieve them in another page 在下面的内容中,我想使表的最后8个条目显示在索引页上。我能够在每个表格的第一个表格中显示产品名称和其他功能,但是我想显示与该帖子的product_id相关联的第一张图片(*图片具有唯一的ID,但product_id是该产品的外键第一个表中的主键-来自产品的product_id)。有人用PHP脚本帮助我吗?

php代码:

$sql = SELECT id, name, quantity, description FROM products ORDER BY id DESC LIMIT 8;
$result = mysqli_query($connection, $sql);

html代码:

 if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
            echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["quantity"]. "<br>";
            echo "<a href='getProduct.php?id=$row[id]'>See Product</a> <br>";
    }
} else { echo "0 results";
}

表格

CREATE TABLE `products` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `quantity` int(11) DEFAULT NULL,
  `description` varchar(150) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `products_images` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `product_id` int(11) unsigned DEFAULT NULL,
  `filename` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `product_id` (`product_id`),
  CONSTRAINT `products_images_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

对不起...是我关于stackoverflow的第一篇文章...

2 个答案:

答案 0 :(得分:1)

有两种方法可以检索图像。第一个是在遍历结果的同时对每个图像运行另一个查询

类似这样的东西:

<?php

$sql = "SELECT id, name, quantity, description FROM products ORDER BY id DESC LIMIT 4";
$result = mysqli_query($connection, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["quantity"]. "<br>";
        echo "<a href='getProduct.php?id=$row[id]'>See Product</a> <br>";
        // load the image
        $imageResult = mysqli_query($connection, "SELECT filename FROM products_images WHERE product_id = " . mysqli_real_escape_string($connection, $row["id"]) . " LIMIT 1");
        if (mysqli_num_rows($imageResult) == 1) {
            $imageRow = mysqli_fetch_assoc($imageResult);
            echo "the image filename is: " . $imageRow["filename"];
        }

    }
} else { echo "0 results"; }

第二个选项,我希望是“加入”第二个表,它看起来可能像这样:

<?php

$sql = "SELECT p.id, p.name, p.quantity, p.description, i.filename FROM products p LEFT JOIN product_images i on i.product_id = p.id ORDER BY p.id DESC LIMIT 4";
$result = mysqli_query($connection, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["quantity"]. "<br>";
        echo "<a href='getProduct.php?id=$row[id]'>See Product</a> <br>";
        if ($row["filename"]) {
            echo "the image filename is: " . $row["filename"];
        }

    }
} else { echo "0 results"; }

我建议您先阅读有关SQL中的联接的信息(有很多资源,例如:https://www.w3schools.com/sql/sql_join.asphttps://en.wikipedia.org/wiki/Join_(SQL)http://www.sql-join.com/

接下来,我建议您针对名为SQL Injection的事物保护数据库查询。在第一个示例中,我添加了mysqli_real_escape_string($connection, $row["id"])来执行此操作。更好的方法(通常应该使用这种技术编写数据库查询!)是使用准备好的语句。您可以阅读有关它们的信息。此处:https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection

接下来我要指出的是,您不需要用PHP编写所有内容。

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
            echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["quantity"]; ?><br>
            <a href="getProduct.php?id=<?php echo $row[id]; ?>">See Product</a><br>
    <?php }
} else { echo "0 results"; }

完全正确!无需使用echo输出所有html代码,而只需“暂停”介于两者之间的php。

最后,我想向您介绍https://phptherightway.com/,它不仅涵盖了“基本知识”,而且还为您提供了更多资源。

答案 1 :(得分:0)

如果要恢复与产品关联的图像,则必须使用WHERE克隆或使用JOINS来完成,例如:

SELECT 
    id, 
    name, 
    quantity, 
    description 
FROM products, image 
WHERE products.id = image.id 
ORDER BY id DESC 
LIMIT 4;

或者也是

SELECT 
    id, 
    name, 
    quantity, 
    description 
FROM products 
NATURAL JOIN image 
ORDER BY id DESC 
LIMIT 4;

但这意味着idImage必须作为产品表中的外键