0

I have code for image upload and view in php and MySQL. After click on "Submit" button in "imageUpload.php" page image is stored in database. but not displaying in "listImages.php" page. I don't know what's the problem. I see "image not displaying when uploading in php" but its seems different solution for me. here is my code please have a look where i am wrong.

imageUpload.php :

<?php

 /* CREATE TABLE IF NOT EXISTS `output_images` 
  (
  `imageId` tinyint(3) NOT NULL AUTO_INCREMENT,
  `imageType` varchar(25) NOT NULL DEFAULT '',
  `imageData` mediumblob NOT NULL,
   PRIMARY KEY (`imageId`)
   ) */    

if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysqli_connect("localhost", "root", "");
mysqli_select_db ("test");
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);

$sql = "INSERT INTO output_images(imageType ,imageData)
VALUES('{$imageProperties['mime']}', '{$imgData}')";
$current_id = mysqli_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error());
if(isset($current_id)) {
    header("Location: listImages.php");
}
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>

listImages.php :

<?php
$conn = mysqli_connect("localhost", "root", "");
mysqli_select_db("test");
$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC"; 
$result = mysqli_query($sql);
?>
<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
while($row = mysqli_fetch_array($result)) {
?>
    <img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" /><br/>
<?php       
}
  mysqli_close($conn);
?>
</BODY>
</HTML>

imageView.php :

<?php
$conn = mysqli_connect("localhost", "root", "");
mysqli_select_db("test") or die(mysqli_error());
if(isset($_GET['image_id'])) {
    $sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id'];
    $result = mysqli_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysqli_error());
    $row = mysqli_fetch_array($result);
    header("Content-type: " . $row["imageType"]);
    echo $row["imageData"];
}
mysqli_close($conn);
?>

listimage.php

9
  • What happens if you visit imageView.php?image_id=1 in your browser? Do you see the image? (With a valid image_id) Commented Sep 22, 2016 at 9:16
  • No in listimages.php their is broken icons as seen in above image. when i see code view of listimages.php then in <img> their is "<img src="imageView.php?imageId=8" /><br/>" that means imageView.php getting id properly. Commented Sep 22, 2016 at 9:20
  • I asked about imageView.php not listimages.php Commented Sep 22, 2016 at 9:21
  • @Rolf Pedro Ernst I see "localhost/mysql_blob_using_php/mysql_blob/…" in browser but page is blank their is no image. Commented Sep 22, 2016 at 9:30
  • @John120 right click the broken image and inspect.. see whether the image echo correctly or not Commented Sep 22, 2016 at 9:46

2 Answers 2

1

this would help you

<a href="imageView.php?image_id=<?php echo $row["imageId"]; ?>">
  <img src="<?php echo $row['imagedata']; ?>" alt="my picture" height="128" width="128" />
</a>
Sign up to request clarification or add additional context in comments.

Comments

1

it should be

    $conn=mysqli_connect("ur_servername_ex_localhost","ur_username","ur_password","ur_db");
    mysqli_query($conn, $sql);
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.