Skip to content
Advertisement

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? Here is the complete code. below is the php code and the html code which i designed <?php //for image upload

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='Imagesitem_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>

Advertisement

Answer

i have figured it out. i decided to use

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

instead of the previous

echo "<img src='Imagesitem_img/".$row['item_image']."'>";
7 People found this is helpful
Advertisement