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

时间:2017-08-02 13:04:24

标签: php html sql

我能够设计一个代码来将图像和其他数据上传到数据库,并显示其中的一些数据。一切正常但图像无法显示,并且当动作完成时,消息也不会出现。我怎样才能解决这个问题?这是完整的代码。 下面是我设计的php代码和html代码     

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>

1 个答案:

答案 0 :(得分:0)

我已经弄明白了。我决定使用

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

而不是之前的

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