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