简体   繁体   中英

How do I add images to my PHP blog?

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

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

you need to upload images to a directory and save images name in a database after that right the url of the image in img tag and get the image name from the database

your html form

<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>

your php code

<?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;
    }
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM