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>";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭