简体   繁体   中英

PHP MySQL can't find my image from Database

Im working on a blog site for school where i have to make a posting system with PHP MySQL. I'm now trying to add images to my post, I've already made a image uploader and its completly working. Now i want to make an option button to choose which image i want at a specific blog post, so i tried to make an option button which would show me all the image names i had in my Database, but now it doesnt say anything and the code stops working after my php code in my form. It shows the Title, body and category but then shows an empty option box. The submit button is not showing aswell.

I hope someone could help me out with this problem im having!

I cant post images yet so i'll tell what my DB names are, image_id //id of the image name //name of the image image //blob

I've also put the image_id in my 'post' table.

Code:

<?php
session_start();
if(!isset($_SESSION['user_id'])){
    header('Location: login.php');
    exit();
}

include('../includes/db_connection.php');
if(!isset($_SESSION['user_id'])){
    header('Location: login.php');
    exit();
}

if(isset($_POST['submit'])){
    //get the blog data
    $title = $_POST['title'];
    $body = $_POST['body'];
    $category = $_POST['category'];
    $image = $_POST['name'];
        //link everything to the db
        $title = $db->real_escape_string($title);
        $body = $db->real_escape_string($body);
        $user_id = $_SESSION['user_id'];
        $date = date('Y-m-d G:i:s');
        $body = htmlentities($body);
    //check if all of these are there
    if($title && $body && $category && $image){
        //place data back into the database
        $query = $db->query("INSERT INTO posts (user_id, title, body, category_id, image_id, posted) VALUES('$user_id', '$title', '$body', '$category', '$image', '$date')");
        if($query){
            echo "post added";
        }
        else{
            echo "error";
        }
    }
    else{
        echo "MISSING DATA";
    }
}

?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=9"/>
        <title> Portfolio Sander Mateijsen </title>
            <style>
            #wrapper{
                margin: auto;
                width: 800px;
            }
            label(display:block)
        </style>
    </head>
    <body>


    <div id="wrapper">
        <div id="content">
                <form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post">
                    <label>Titel:</label><br><input type="text" name="title" /><br>
                    <label for="body"> Body:</label><br>
                    <textarea name="body" cols=50 rows=10></textarea><br>
                    <label> Category:</label><br>
                    <select name="category">
                        <?php 
                            //display categories from the database as options
                            $query = $db->query("SELECT * FROM categories");
                            while($row = $query->fetch_object()){
                                echo "<option value='".$row->category_id."'>".$row->category."</option>";
                            }
                        ?>
                    </select>
                    <label> Image:</label><br>
                    <select name="name">
                        <?php 
                            //display images from the database as options
                            $query = $db->query("SELECT * FROM blob");
                            while($row = $query->fetch_object()){
                                echo "<option value='".$row->image_id."'>".$row->name."</option>";
                            }
                        ?>
                    </select>
                    <br><br>
                    <input type="submit" name="submit" value="submit" />
                </form>
                <a href="overzicht_post.php"> Terug naar overzicht.</a>
            </div>

        </div>
    </div>
</body>
</html>

if($title && $body && $category && $image){} checking boolean values on your code.

Change this line to

if(isset($title) && isset($body) && isset($category) && isset($image)){}

Also

$query = $db->query("INSERT INTO posts (user_id, title, body, category_id, image_id, posted) VALUES('$user_id', '$title', '$body', '$category', '$image', '$date')");

is wrong, use prepare and execute methods for insert and update and bind values

$query = $db->prepare("INSERT INTO posts (user_id, title, body, category_id, image_id, posted) VALUES(:user_id, :title, :body, :category, :image, :date)");

$query->bindParam(":user_id",$user_id);
$query->bindParam(":title",$title);
$query->bindParam(":body",$body);
$query->bindParam(":category",$category);
$query->bindParam(":image",$image);
$query->bindParam(":mydate",$date); //dont use date as bindparam because it is a special name. 

$query->execute();

Also you're not passing posted . I assumed you have default value on your sql column for this field.

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