gpt4 book ai didi

php - 如何从MySQL数据库检索文本和图像以显示在网页上

转载 作者:行者123 更新时间:2023-11-29 16:11:45 24 4
gpt4 key购买 nike

我正在设计一个网站,并有一个可由管理员编辑的新闻区域。有一个标题和条目字段,条目存储在 MySQL 数据库中。我还尝试添加一个选项,允许管理员添加图像(甚至可能是视频)。我可以将图像正确存储到数据库中(我相信),但是当尝试检索它以在我的网页上显示时,它显示为一堆随机数字、字母和问号符号。谁能帮我找出我做错了什么?

这是我的表单以及将数据插入数据库的代码:

        $title = trim(strip_tags($_POST['title']));
$entry = trim(strip_tags($_POST['entry']));
$image = ($_POST['image']);
$imgContent = addslashes(file_get_contents($image));

$problem = TRUE;
if ($problem) {
// Define the query:
$query = "INSERT INTO entries (entry_id, title, entry, date_entered, image) VALUES (0, '$title', '$entry', NOW(),'$imgContent')";
// Execute the query
if(@mysqli_query($dbc, $query) && ((!empty($_POST['entry'])) || (!empty($_POST['title'])) )) {
print '<p>The blog entry has been added!</p>';
}else{
print '<p style="color: red;">Could not add the entry. Please fill in one of the fields.</p>';
}
}
mysqli_close($dbc);

} // End of form submission IF

print '<form action="add_entry.php" method="post">
<p>Entry Title: <input type="text" name="title" size="40" maxsize="100" /></p>
<p>Entry Text: <textarea name="entry" cols="40" rows="5"></textarea></p>
<p><input type="file" name="image" /></p>
<input type="submit" name="submit" value="Post This Entry!" />
<input type="hidden" name="submitted" value="true" />
</form>';

以下是查看新闻页面数据的代码:

// Define the query:
$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 "{$row['image']}
<dl><dt><h3><strong>{$row['title']}</strong></h3></dt>
<dd>{$row['entry']}<br /><br />\n</dd></dl>";
}

更新:我可以让它返回文件路径,但它没有斜杠。有没有一种简单的方法来添加这些斜杠,然后我可以将其返回到图像标签中?

最佳答案

$image = ($_FILES['image']);

<form action="add_entry.php" method="post" enctype= multipart/form-data>

并从数据库中检索图像路径并将其放入img src=""标签

如果您使用file_get_contents($image),您可以将图像转换为blob()

像这样更正您的代码。

关于php - 如何从MySQL数据库检索文本和图像以显示在网页上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55193498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com