尝试使用 PHP 将数据库中的图像插入到每个条目上方的新闻博客中



我正在创建一个网站,并希望允许管理员添加从MySQL数据库中提取的图像,并将其显示在正在添加的新闻博客文本上方。我可以让它显示图像和文本,但它们被分组在一起(带有图像的图像和带有文本的文本(。如何让我的网站显示最后添加的文本条目上方输入的最后一个图像?

<?php
$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC";
$result = mysqli_query($dbc, $sql);
?>
</BODY>
</HTML>
<div class="brown-container-fluid text-left">
    <div class="text-home">
        <h2><strong>Pine Lane News</strong></h2><br/>
        <?php
        if ($row = mysqli_fetch_array($result)) {
            ?>
            <div style="text-align:center;">
                <img style="max-width:300px; max-height:300px;"
                     src="imageView.php?image_id=<?php echo $row["imageId"]; ?>"/><br/>
            </div>
            <?php
        }
        $query = 'SELECT * FROM entries ORDER BY date_entered DESC';
        if ($r = mysqli_query($dbc, $query)) { // Run the query.
            // Retrieve and print every record:
            while ($row = mysqli_fetch_array($r)) {
                print
                    "<dl><dt><h3><strong>{$row['title']}</strong></h3></dt>
    <dd>{$row['entry']}<br /><br />n</dd></dl>";
            }
        } else { // Query didn't run.
            print '<p style="color: red;">Could not retrieve the data because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
        } // End of query IF.
        mysqli_close($dbc); // Close the database connection.
        ?>
    </div>
</div>

我想出的东西非常快,没有经过测试。

<?php
        while($row = mysqli_fetch_array($result)){
            echo '<div style="text-align:center;">';
            echo '<img style="max-width:300px; max-height:300px;"';
            echo 'src="imageView.php?image_id="'.$row["imageId"].'"/><br/>';
            echo '</div>';
            $query = 'SELECT * FROM entries ORDER BY date_entered DESC LIMIT 1';
            if ($r = mysqli_query($dbc, $query)) { // Run the query.
                while ($row = mysqli_fetch_array($r))
                {
                print
                    "<dl><dt><h3><strong>{$row['title']}</strong></h3></dt>
    <dd>{$row['entry']}<br /><br />n</dd></dl>";
            }
        } else { // Query didn't run.
            print '<p style="color: red;">Could not retrieve the data because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
        } // End of query IF.
        mysqli_close($dbc); // Close the database connection.
        ?>

最新更新