簡體   English   中英

PHP MySQL無法從數據庫中找到我的圖像

[英]PHP MySQL can't find my image from Database

我在學校的博客網站上工作,我必須使用PHP MySQL建立一個發布系統。 我現在正在嘗試將圖像添加到我的帖子中,我已經制作了一個圖像上傳器並且它完全正常工作。 現在我想做一個選項按鈕,在特定的博客文章中選擇我想要的圖像,所以我試着制作一個選項按鈕,它會顯示我在我的數據庫中的所有圖像名稱,但現在它沒有說什么和代碼在我的表單中的PHP代碼后停止工作。 它顯示標題,正文和類別,但隨后顯示一個空選項框。 提交按鈕也不顯示。

我希望有人可以幫助我解決這個問題!

我無法發布圖片,所以我會告訴我的數據庫名稱是什么,image_id //圖像名稱的id //圖像圖像的名稱// blob

我還把image_id放在我的'post'表中。

碼:

<?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){}檢查代碼的布爾值。

將此行更改為

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

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

是錯誤的,使用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();

你也沒有通過posted 我假設你在這個字段的sql列上有默認值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM