显示带有用户产品发布信息的图像?

时间:2020-02-01 07:44:42

标签: php image

我正在创建craigslist之类的网站,人们可以在其中发布要出售的商品,并附上帖子标题,帖子说明和电子邮件。

问题: 除了图像,我可以从数据库中提取所有数据。上载图像后,图像将存储在C:\ xampp \ htdocs \ uploads中,图像名称与其余信息一起保存在数据库中。下面是我的PHP。我试图从数据库中提取图像的名称,并使用它来像这样

echo "<img src='uploads/".$image."' width='200'> ";

我是php的新手,所以不胜感激。这是存储/检索用户上传图像的一种好方法吗? 谢谢

<?php

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "mydb"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql= "SELECT * FROM products";
$result = $con->query($sql);
$image = "SELECT image FROM products";


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<img src='uploads/".$image."' width='200'> ";
        echo "Title: " . $row["title"]. " Price: $" . $row["price"]. " <br> " . $row["description"]. " <br> " . $row["contact"] . " <br><br>";
    }
} else {
    echo "0 results";
}
$con->close();


?>

1 个答案:

答案 0 :(得分:0)

您需要对图像使用相同的行:

$sql= "SELECT * FROM products";
$result = $con->query($sql);



if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<img src='uploads/".$row["image"]."' width='200'> ";
        echo "Title: " . $row["title"]. " Price: $" . $row["price"]. " <br> " . $row["description"]. " <br> " . $row["contact"] . " <br><br>";
    }
} else {
    echo "0 results";
}
$con->close();