当你从mysql数据库上传图片时,如何用图片显示最后5篇文章?

bmvo0sr5  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(343)

总体目标是在上传到mysql数据库时显示最后5篇文章的图片。我有4个文件:
php-连接到数据库-非常好用
新的_post.php-你可以添加到数据库-工作很好
upload.php-上传到数据库(图像和内容)-非常好
index.php-显示所有内容的基于家庭的文件有问题的文件
我不明白的主要问题是为什么在index.php中会看到多张卡?我猜可能是while循环和bootstrap冲突。我在mysql中只有4个数据。
以下是index.php:

<?php
require 'conexion.php';
$conexion = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
?>

<!DOCTYPE html>
<html lang="es" dir="ltr">
<head>
    <meta charset="utf-8">
    <title>Blog</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
          integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    <link rel="stylesheet" href="css/estilos.css">
</head>
<body>
<img src="https://placeimg.com/720/200/arch" id="banner"/>

<div class="container" id="principal">
    <h1>Bienvenido a mi blog!</h1>
    <a href="new_post.php" class="btn btn-primary btn-add">Agregar post</a>
    <h2>Ultimos 5 posts</h2>
    <div id="card-list">

        <div class="card">
            <?php
            $sql = "SELECT * FROM blogtp_1";
            $result = mysqli_query($conexion, $sql);
            while ($row = mysqli_fetch_array($result)) {
                echo  '<img class="card-img-top" src="imagen/' . $row["imagen_upload"] . '"/>
                       <div class="card-body">
                         <h5 class="card-title">' . $row["titulo_del_post"] . '</h5>
                         <p class="card-text">' . $row["contenido_del_post"] . '</p>
                         <a href="ver.php?id=' . $row["id"] . '" class="btn btn-primary">Ver</a>
                       </div> ';
            };
            ?>
        </div>

        <div class="card">
            <?php
            $sql = "SELECT * FROM blogtp_1";
            $result = mysqli_query($conexion, $sql);
            while ($row = mysqli_fetch_array($result)) {
                echo  '<img class="card-img-top" src="imagen/' . $row["imagen_upload"] . '"/>
                       <div class="card-body">
                         <h5 class="card-title">' . $row["titulo_del_post"] . '</h5>
                         <p class="card-text">' . $row["contenido_del_post"] . '</p>
                         <a href="ver.php?id=' . $row["id"] . '" class="btn btn-primary">Ver</a>
                       </div> ';
            };
            ?>
        </div>

        <div class="card">
            <?php
            $sql = "SELECT * FROM blogtp_1";
            $result = mysqli_query($conexion, $sql);

            while ($row = mysqli_fetch_array($result)) {
                echo  '<img class="card-img-top" src="imagen/' . $row["imagen_upload"] . '"/>
                       <div class="card-body">
                         <h5 class="card-title">' . $row["titulo_del_post"] . '</h5>
                         <p class="card-text">' . $row["contenido_del_post"] . '</p>
                         <a href="ver.php?id=' . $row["id"] . '" class="btn btn-primary">Ver</a>
                       </div> ';
            };
            ?>
        </div>

        <div class="card">
            <?php
            $sql = "SELECT * FROM blogtp_1";
            $result = mysqli_query($conexion, $sql);

            while ($row = mysqli_fetch_array($result)) {

                echo  '<img class="card-img-top" src="imagen/' . $row["imagen_upload"] . '"/>
                       <div class="card-body">
                         <h5 class="card-title">' . $row["titulo_del_post"] . '</h5>
                         <p class="card-text">' . $row["contenido_del_post"] . '</p>
                         <a href="ver.php?id=' . $row["id"] . '" class="btn btn-primary">Ver</a>
                       </div> ';
            };
            ?>
        </div>

        <div class="card">
            <?php
            $sql = "SELECT * FROM blogtp_1";
            $result = mysqli_query($conexion, $sql);

            while ($row = mysqli_fetch_array($result)) {
                echo  '<img class="card-img-top" src="imagen/' . $row["imagen_upload"] . '"/>
                       <div class="card-body">
                         <h5 class="card-title">' . $row["titulo_del_post"] . '</h5>
                         <p class="card-text">' . $row["contenido_del_post"] . '</p>
                         <a href="ver.php?id=' . $row["id"] . '" class="btn btn-primary">Ver</a>
                       </div> ';
            };
            ?>
        </div>
    </div>
</div>
</body>
</html>

如果你能帮助我,我将非常感激!

bmp9r5qi

bmp9r5qi1#

您只需要迭代一次,然后在不同的卡片中显示每个帖子。
索引.php

<?php

require 'conexion.php';

$conexion = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

function show_posts() {
    global $conexion;
    $sql = "SELECT * FROM blogtp_1";
    $result = mysqli_query($conexion, $sql);
    while ($row = mysqli_fetch_array($result)) {
        echo '<div class="card">
                <img class="card-img-top" src="imagen/' . $row["imagen_upload"] . '"/>
                <div class="card-body">
                     <h5 class="card-title">' . $row["titulo_del_post"] . '</h5>
                     <p class="card-text">' . $row["contenido_del_post"] . '</p>
                     <a href="ver.php?id=' . $row["id"] . '" class="btn btn-primary">Ver</a>
                </div> 
              </div>';
    };
}
?>

<!DOCTYPE html>
<html lang="es" dir="ltr">
<head>
    <meta charset="utf-8">
    <title>Blog</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
          integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    <link rel="stylesheet" href="css/estilos.css">
</head>
<body>
<img src="https://placeimg.com/720/200/arch" id="banner"/>

<div class="container" id="principal">
    <h1>Bienvenido a mi blog!</h1>
    <a href="new_post.php" class="btn btn-primary btn-add">Agregar post</a>
    <h2>Ultimos 5 posts</h2>
    <div id="card-list">
        <?php show_posts(); ?>
    </div>
</div>
</body>
</html>

相关问题