【问题标题】:PHP MySQL can't find my image from DatabasePHP MySQL 无法从数据库中找到我的图像
【发布时间】:2015-08-27 04:23:15
【问题描述】:

我正在为学校开发一个博客网站,我必须使用 PHP MySQL 制作一个发布系统。我现在正在尝试将图像添加到我的帖子中,我已经制作了一个图像上传器并且它可以完全正常工作。现在我想制作一个选项按钮来在特定的博客文章中选择我想要的图像,所以我尝试制作一个选项按钮,它会显示我在数据库中拥有的所有图像名称,但现在它什么也没说,而且在我的表单中的 php 代码之后,代码停止工作。它显示标题、正文和类别,但随后显示一个空选项框。提交按钮也没有显示。

我希望有人能帮我解决我遇到的这个问题!

我还不能发布图片,所以我会告诉我我的数据库名称是什么, image_id //图片的id name //图片名称 图片 //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>

【问题讨论】:

  • 你尝试了哪些转储?
  • 抱歉,转储是什么意思?
  • var_dump($array) 返回数组的所有元素。那么你的数组的输出是什么?
  • 我试过var_dump($query),但没有任何显示。

标签: php mysql image forms


【解决方案1】:

if($title &amp;&amp; $body &amp;&amp; $category &amp;&amp; $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 列上有默认值。

【讨论】:

  • 我已经尝试了你所做的,仍然没有发生任何事情,它仍然是一样的,我也没有在控制台中收到任何错误。 'posted' 也是我的数据库中存储 $date 的名称。
  • 尝试更改 $query 变量名称以用于不同目的,例如 $query1、$query2
猜你喜欢
  • 2019-02-22
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
相关资源
最近更新 更多