【发布时间】:2019-07-26 00:23:52
【问题描述】:
我正在尝试将 3 张图像限制为一行。只有一行,但是,我真的不明白如何限制要显示的图像数量以及如何将最新图像显示到第一行,它只显示最新到最后一行。我正在尝试某种方法,但没有任何效果。
上传
<?php
if (isset($_POST['submit'])){
$target_dir = dirname(dirname(__FILE__)).'/store/';
$target = $target_dir . basename($_FILES['image']['name']);
$db = mysqli_connect("localhost", "root", "", "image");
$image = $_FILES['image']['name'];
$title = $_POST['image_title'];
$description = $_POST['description'];
$sql = "INSERT INTO images (image, title, description) VALUE ('$image', '$title', '$description')";
mysqli_query($db, $sql);
if(move_uploaded_file($_FILES['image']['tmp_name'], $target))
{
// header("location:../../gallery/gallery.html");
header("location: ../test.php");
}
}
?>
显示
<?php
$db = mysqli_connect("localhost", "root", "", "image");
$sql = "SELECT * FROM images";
$result = mysqli_query($db, $sql);
$count = 0;
while($row = mysqli_fetch_array($result)){
$listImage = $row['image'];
echo "<img src='store/".$listImage."' width='250' height='250'>";
}
【问题讨论】:
-
图像表中是否有自动增量
id字段或created_at列? -
您必须在表中使用
DATETIME列,并使用该字段ORDER BY DESC获取最新图像。 -
不要忘记
LIMIT 1或任何你想要的。 -
@ArtisticPhoenix 为什么要加
LIMIT 1?他/她想获取顶部的所有行和最新的行,然后是其他行。 -
@ArtisticPhoenix 这更多地取决于用例,因为对主键进行排序不会造成太大伤害。让这个用户更新问题中的表结构,使其更清晰。