首页 > 解决方案 > 从外键表中获取第一张图片

问题描述

我在这里的线程的帮助下创建了一个发布系统:添加带有详细信息(名称、数量、多个图像)的产品并在另一个页面中检索它们在 下面我想让表格的最后 8 个条目显示在索引页。我能够在第一个表中显示每个产品的名称和其他功能,但我想显示与该帖子的 product_id 关联的第一张图像(*图像​​具有唯一的 id,但 product_id 是外键第一个表中的主键 - products 中的 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上的第一篇文章...

标签: phphtmlmysql

解决方案


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

像这样的东西:

<?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.asp,https: //en.wikipedia.org/wiki/Join_( SQL)http://www.sql-join.com/ )

接下来,我向您推荐的是保护您的数据库查询不受称为SQL 注入的事物的影响。在第一个示例中,我添加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中间的“暂停” php 内容来输出所有 html 代码。

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


推荐阅读