php - 从外键表中获取第一张图片
问题描述
我在这里的线程的帮助下创建了一个发布系统:添加带有详细信息(名称、数量、多个图像)的产品并在另一个页面中检索它们在 下面我想让表格的最后 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上的第一篇文章...
解决方案
有两种方法可以检索图像。第一个是为每个图像运行另一个查询,同时您正在遍历结果
像这样的东西:
<?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/,它不仅涵盖了“基础知识”,还为您提供了更多资源。
推荐阅读
- html - 如何使用 css nth-child(-n+3) 仅显示列表中的前三个图像
- python - 随机值张量上的计算(例如 tf.greater 和 tf.cond)未按预期工作
- python - 许多属性的内联循环命名元组
- sql - 如何从 SQL 中带有 WHERE 条件的下一条记录中获取值?
- symfony - 如何在 Sonata configureListFields 中检索主题而不是行动?
- proxy - 使用 Burp 拦截 http 请求不起作用?
- docker - 如何对在 docker swarm 中创建的 docker 服务应用程序进行 nginx 反向代理?
- javascript - onClick 在两个不同的按钮上更改显示(React.js)
- javascript - 如何使用节点服务器制作实时音频流?
- android - 添加片段的通用方法,如何实现