简体   繁体   English

PHP MySQL无法从数据库中找到我的图像

[英]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. 我在学校的博客网站上工作,我必须使用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. 现在我想做一个选项按钮,在特定的博客文章中选择我想要的图像,所以我试着制作一个选项按钮,它会显示我在我的数据库中的所有图像名称,但现在它没有说什么和代码在我的表单中的PHP代码后停止工作。 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 我无法发布图片,所以我会告诉我的数据库名称是什么,image_id //图像名称的id //图像图像的名称// blob

I've also put the image_id in my 'post' table. 我还把image_id放在我的'post'表中。

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. if($title && $body && $category && $image){}检查代码的布尔值。

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 是错误的,使用prepareexecute方法进行插入和更新以及绑定值

$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 . 你也没有通过posted I assumed you have default value on your sql column for this field. 我假设你在这个字段的sql列上有默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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