简体   繁体   English

我无法使用 php 显示/访问 mysql 数据库中的图像

[英]i can't display/access images from the mysql database using php

I was able to design a code to upload image and other data to the database and also display some of those data.我能够设计一个代码来将图像和其他数据上传到数据库并显示其中的一些数据。 Everything works but the image doesn't display and the messages too don't come up when the action is completed or not.一切正常,但图像不显示,并且在操作完成与否时也不会出现消息。 How can I display/access the stored images from mysql database?如何显示/访问 mysql 数据库中存储的图像? Here is the complete code.这是完整的代码。 below is the php code and the html code which i designed <?php //for image upload下面是我设计的 php 代码和 html 代码 <?php //用于图片上传

session_start();
$_SESSION['message']="";


$mysqli = new mysqli('localhost', 'root', '','auction');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$item_name = $mysqli->real_escape_string($_POST['item_name']);
$item_description = $mysqli->real_escape_string($_POST['item_description']);
$item_image_path = $mysqli-
>real_escape_string('Images/item_img/'.$_FILES['item_image']['name']);

//make sure file is of image type
if (preg_match("!image!", $_FILES['item_image']['type'])) {
    if (copy($_FILES['item_image']['tmp_name'], $item_image_path)) {
        $_SESSION['item_name'] = $item_name;
        $_SESSION['item_description'] = $item_description;  
        $_SESSION['item_image'] = $item_image_path;
        $sql = "INSERT INTO items (item_name, item_image_path, 
item_description)
                VALUES('$item_name', 
 '$item_image_path','$item_description')";
        if ($mysqli->query($sql) == true) {
            $_SESSION['message'] = "Item Upload Successful!";
        } else {
            $_SESSION['message'] = "file upload failed";
        }
        
    }
    else{
        $_SESSION['message'] = "file copying failed";
}
    }
     else {
    $_SESSION['message'] = "please upload gif, jpg, png";
}

}

?>

<html>
<head>
    <title>Upload item</title>
    <link rel="StyleSheet" href="Bootstrap/css/bootstrap.main.css">
    <link rel="StyleSheet" href="Bootstrap/css/bootstrap.min.css">
    <link rel="StyleSheet" href="style.css">

</head>
<body>
    <div>
        <div>
            <?php
                $mysqli = new mysqli('localhost','root','','auction');
                $sql = "SELECT * FROM items ";
                $result = mysqli_query($mysqli, $sql);
                while ($row = mysqli_fetch_array($result)) {
                
                    echo "<img 
src='Images\item_img/".$row['item_image']."'>";
                    echo "<p>".$row['item_name']."</p>";
                    
                }
            ?>
        </div>
        <form class="form-horizontal" role="form" 
action="auction_upload.php" method="POST" enctype="multipart/form-data">
            <h1><? = $_SESSION['message'];?></h1>
            <div class=" form-group">
        <label class="control-label col-sm-2">Item Name:</label>
        <div class="col-sm-8">
            <INPUT type="text" class="form-control" name="item_name" 
required/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-2">Item Image:</label>
        <div class="col-sm-8">
            <INPUT type="file" class="form-control" name="item_image" 
accept="image/*" required/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-2">Item Description:</label>
        <div class="col-sm-8">
            <textarea class="form-control" name="item_description" required>
</textarea>
        </div>
    </div>

<div class="form-group"> 
  <div class="col-sm-offset-2 col-sm-8"> 
     <button type="submit" class="btn btn-default" 
name="upload">Upload</button>
  </div> 
</div> 
        </form>
    </div>
</body>
</html>

i have figured it out.我想通了。 i decided to use我决定使用

 echo "<img src='$row[item_image]'>";

instead of the previous而不是以前的

echo "<img src='Images\item_img/".$row['item_image']."'>";

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

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