【问题标题】:i can't display/access images from the mysql database using php我无法使用 php 显示/访问 mysql 数据库中的图像
【发布时间】:2017-08-02 13:04:24
【问题描述】:

我能够设计一个代码来将图像和其他数据上传到数据库并显示其中的一些数据。一切正常,但图像不显示,并且在操作完成与否时也不会出现消息。如何显示/访问 mysql 数据库中存储的图像?这是完整的代码。 下面是我设计的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>

【问题讨论】:

  • 好的,那么C:\xampp2\htdocs\trail\auction_upload.php 的第 10 行和第 13 行是什么?
  • 你必须忘记声明图像变量或者你正在使用另一个变量名
  • 这里是完整的源代码。
  • 对不起,我发现很难在 stackoverflow 中包含代码。我决定包含屏幕截图的链接。
  • 代码作为图像是没有用的。没有人可以从中复制文本来进行任何调试。请编辑您的问题并将代码添加到其中。

标签: php html sql


【解决方案1】:

我已经想通了。我决定使用

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

而不是以前的

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

【讨论】:

    猜你喜欢
    • 2019-02-22
    • 1970-01-01
    • 2015-04-06
    • 2018-04-29
    • 2019-03-19
    • 2020-06-22
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多