douxian6260 2017-01-26 13:06
浏览 19

如何将图像添加到我的PHP博客?

I have made a blog for my website with PHP and mysql database, where I can add blog posts from an admin site (www.website.com/admin) and display them on my website (www.website.com). It's working fine, but I want to add pictures too.

This is my code for adding:

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();

And my code for display the blog posts

/.../
while ($row = $result->fetch_object()) {

    echo "<div>";
    echo "<td>" . $row->blogtitle . "</td>";
    echo "<td>" . $row->blogdate . "</td>";
    echo "<td>" . $row->blogdesc . "</td>";
    echo "</div>";
 }

I know how to make an upload.php, but is it easier to upload to mysql? I dont know how to get the image shown in the right blogpost after uploading.

Best regards, Tobias Dybdahl

  • 写回答

2条回答 默认 最新

  • drmqzb5063 2017-01-26 13:12
    关注

    You can upload the file to the server and then store the filename in your database, for example a column named "blogimg".

    Then in your code for displaying blog posts you can add this line to show the image:

    echo "<td><img src='" . $row->blogimg . "' /></td>";
    
    评论

报告相同问题?