如何将图像添加到我的PHP博客?
我用PHP和mysql数据库为我的网站创建了一个博客,在那里我可以从管理员网站(www.website.com/admin)添加博客文章,并将它们显示在我的网站(www.website.com)上。它工作正常,但我也想添加图片 这是我添加的代码:如何将图像添加到我的PHP博客?,php,mysql,sql,Php,Mysql,Sql,我用PHP和mysql数据库为我的网站创建了一个博客,在那里我可以从管理员网站(www.website.com/admin)添加博客文章,并将它们显示在我的网站(www.website.com)上。它工作正常,但我也想添加图片 这是我添加的代码: if (isset($_POST['submit'])) { $blogtitle = htmlentities($_POST['blogtitle'], ENT_QUOTES); $blogdate = htmlentities($
if (isset($_POST['submit'])) {
$blogtitle = htmlentities($_POST['blogtitle'], ENT_QUOTES);
$blogdate = htmlentities($_POST['blogdate'], ENT_QUOTES);
$blogdesc = htmlentities($_POST['blogdesc'], ENT_QUOTES);
// check that firstname and lastname are both not empty
if ($blogtitle == '' || $blogdesc == '') {
$error = 'Please fill in all required fields';
renderForm($blogtitle, $blogdesc, $error);
} else {
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT blog_posts (blogtitle, blogdate, blogdesc) VALUES (?, ?, ?)")) {
$stmt->bind_param("sss", $blogtitle, $blogdate, $blogdesc);
$stmt->execute();
$stmt->close();
} else {
echo "ERROR: Could not prepare SQL statement.";
}
header("Location: website.php");
}
} else {
renderForm();
}
}
// close the mysqli connection
$mysqli->close();
和我的显示博客文章的代码
/.../
while ($row = $result->fetch_object()) {
echo "<div>";
echo "<td>" . $row->blogtitle . "</td>";
echo "<td>" . $row->blogdate . "</td>";
echo "<td>" . $row->blogdesc . "</td>";
echo "</div>";
}
//
而($row=$result->fetch_object()){
回声“;
回显“$row->blogtitle.”;
回显“$row->blogdate.”;
echo“$row->blogdesc.”;
回声“;
}
我知道如何创建upload.php,但是上传到mysql更容易吗?我不知道上传后如何在正确的博客帖子中显示图片
致以最良好的祝愿,
Tobias Dybdahl您可以将文件上载到服务器,然后将文件名存储在数据库中,例如名为“blogimg”的列 然后,在显示博客文章的代码中,可以添加以下行以显示图像:
echo "<td><img src='" . $row->blogimg . "' /></td>";
echo“blogimg.”“/>”;
您需要将图像上载到目录,并将图像名称保存到数据库中
然后在img标记中右键单击图像的url,并从数据库中获取图像名称
您的html表单
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
选择要上载的图像:
您的php代码
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
通常,博客允许在描述中包含HTML、标记或类似内容,以便在文本中添加图像。所见即所得编辑器(如CKEditor或TinyMCE)这会很容易。至于上传图片,看看文件管理器。如果你用谷歌搜索,有很多免费的。如果他想再次使用图片,使用WYSIWYG编辑器是不好的。另一篇文章你收到了很多反对票,因为这个问题有太多的解决方案。你基本上是要求我们做你的工作对于你来说,简单地把另一列添加到名为IMANEULL的数据库中,它指向一个在线图像,然后在你的输出中引用另一个<代码> TD <代码>标签中的URL,然后在那里工作。一旦你有了如何显示图像的想法,那么就要担心如何上传它们。关于如何在MySql中存储/检索图像以及如何在PHP中显示它们,我已经有了大量的在线资源。上传时如何在数据库中存储URL名称?如果在提交表单时这样做,可以将其添加到insert语句中。如果在其他位置这样做,可以使用update语句