有问题上传图像显示在输出页面PHP上

问题描述 投票:-1回答:1

我无法使用其他基于文本的信息在输出页面上显示上传的图像。我对使用PHP非常陌生,我正在开展一个练习项目,使用户能够上传有关产品的博客文章。

有一个表单,用户提交信息和上传的图像。该信息将发布到包含博客帖子的另一个页面。我的问题是让图像显示在页面上,其余的输出。

我可能犯了简单,非常愚蠢的错误。但看了这么久之后,我无法自己解决这个问题。我试过谷歌搜索我能想到的“上传和显示图像PHP”领域,没有任何帮助。

如果有人能帮助我指出正确的方向,我将非常感激。

上传表单有一页,newpost.php

这是PHP的用途:

    <?php
session_start();
$msg = "";
include('../includes/db_connect.php');
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}
if(isset($_POST['submit'])){

    $target ="images/".basename($_FILES['image']['name']);

    $title = $_POST['title'];
    $price = $_POST['price'];
    $description = $_POST['description'];
    $category = $_POST['category'];
    $uploads_dir = 'image/';
    $image = basename($_FILES['image']['name']);
    $title = $db->real_escape_string($title);
    $price = $db->real_escape_string($price);
    $description = $db->real_escape_string($description);
    $user_id = $_SESSION['user_id'];
    $description = htmlentities($description);

    if($title && $price && $description && $category && $image){
        $query = $db->query("INSERT INTO post (user_id, title, price, description, category_id, image) VALUES('$user_id', '$title', '$price', '$description', '$category', '$image')");
        if($query){
            echo "product posted";
        }else{
            echo "error";
        }
    }else{
        echo "missing data";
    }

    if(move_uploaded_file($_FILES['image']['tmp_name'], $target)){
        $msg = "Image uploaded successfully";
    }else{
        $msg = "There was an error uploading the file";
    }
}

?>

这是HTML表单:

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" enctype="multipart/form-data">
        <div class="form-group">
            <label>TITLE *</label>
            <input type="text" name="title" class="form-control" 
            placeholder="Product title here" required>
        </div>
        <div class="form-group">
            <label>PRICE *</label>
            <input type="text" name="price" class="form-control" 
            placeholder="Enter with the following format: 0.00 " required>
        </div>
        <div class="form-group">
            <label>CATEGORY</label>
            <select name="category" class="form-control">
               <?php
                   $query = $db->query("SELECT * FROM categories");
                   while($row = $query->fetch_object()){
                       echo "<option value='".$row->category_id."'>".$row->category."</option>";
                       }
               ?>
            </select>
        </div>
        <div class="form-group">
            <label>PRODUCT DESCRIPTION *</label>
            <textarea type="textarea" name="description" class="form-control" placeholder="Write a description of the product here" required></textarea>
        </div>
        <div class="form-group">
            <label>IMAGE(S) *</label>
            <input type="hidden" name="size" value="1000000">
            <input type="file" name="image">
        </div>
        <div class="required">
            * indicates a required field
        </div>
        <button type="submit" name="submit" value="Submit" class="btn btn-default">POST PRODUCT</button>
</form>

现在这里是帖子显示页面的PHP:

<?php
include('includes/db_connect.php');

$query = $db->prepare("SELECT post_id, title, price, description, image FROM post");
$query->execute();
$query->bind_result($post_id, $title, $price, $description, $image);

?>

到目前为止,这是我的HTML。这就是我的问题所在。

<?php
    while ($query->fetch()):
?>
<article>

    <h2><?php echo $title?></h2>
    <p>$<?php echo $price?></p>
    <p><?php echo $description?></p>
    <!-- I'm trying to get my image to show here and am having issues with that. The above information is visible and working as it should. The images just show up as broken image icons /-->
    <p><?php echo "<img src='images/".$image."' >";?></p>

 </article>

 <?php endwhile?> 

这是我正在为帖子工作的sql表:

CREATE TABLE `post` (
  `post_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `price` text NOT NULL,
  `category_id` int(11) NOT NULL,
  `description` text NOT NULL,
  `posted` datetime NOT NULL,
  `image` varchar(300) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

感谢任何看过这个优惠建议的人。对此,我真的非常感激。

php
1个回答
0
投票

不应该是:<p><?php echo "<img src='post/".$image."' >";?></p>

  1. 您是否看过图像存在于'images'目录中?
  2. 你有没有看过数据库,那里的图像是否正确?你可以使用phpmyadmin。
© www.soinside.com 2019 - 2024. All rights reserved.