8 Replies - 2462 Views - Last Post: 05 March 2012 - 09:41 AM Rate Topic: -----

#1 skitband  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-March 12

image display stored in database

Posted 04 March 2012 - 09:08 PM

im having a problem with my code. can pls someone help me with this...
i already stored a particular image in my database.but the problem is how can i display it in another page
i have this codes.


this is the uploading script direct to the database

added.php

<?php

$type = $_POST['type'];
$size = $_POST['size'];
$quantity = $_POST['quantity'];
$prize = $_POST['prize'];
$file = $_FILES['image']['tmp_name'];

if (isset($_POST['submit']))
{
	include('sql.php');
	if (!isset($file)){
	
	echo "Please Select an Image";
	}
	else{
	
	$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
	$image_name = addslashes($_FILES['image']['name']);
	$image_size = getimagesize($_FILES['image']['tmp_name']);
	
	if(!$image_size){
	echo "Invalid File Type";}
	else{
		
	$query= "INSERT INTO tbcatalog VALUES('','$image_name','$image','$type','$size','$quantity','$prize')";	
	$result = @mysql_query($query);
	$lastid = mysql_insert_id();
	echo "Product Successfully Added<img src=view.php?id=$lastid>";
	mysql_close();
	
	}
		}
	}

?>





and this is the page where i can view the uploaded image

view.php

<html>
<head>
<link rel="icon" href="images/logo1.png" type="images/png	"> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white"><table align="center" border="1" width="100%" height="25%"><td><img src="images/images-2.jpeg" width="1020" height="290"></td></table>
<div class="style1" align="left">
<p class="title">Products</p>
<?php
// Script name: view.php
// Description: The display/view all records in the database

include('sql.php');

$id = addslashes($_REQUEST['id']);
$query = "select * FROM tbcatalog where id =".$_GET['id'];
$result = @mysql_query($query);

mysql_close();
?>

<table class="table3" border="1" cellspacing="3" cellpadding="3">
    <tr>
    <th class="text2">#</th><th class="text2">ID</th><th class="text2">Product Name</th><th class="text2">Image</th><th class="text2">Product Type</th><th class="text2">Size</th><th class="text2">Stock</th><th class="text2">Prize</th><th class="text2">Task</th>
	</tr>
<?php
header("Content-type: image/jpeg");
$i=1;
while ($row = mysql_fetch_assoc($result)){


    
    echo "<tr><td bgcolor=\"white\">".$i."</td>";
    echo "<br";
    echo "<tr><td bgcolor=\"white\">".$row['id']."</td>";
    echo "<br";
    echo "<tr><td bgcolor=\"white\">".$row['name']."</td>";
    echo "<br";
    echo "<tr><td bgcolor=\"white\"><img src=".$row['image']."></td>";
    echo "<br";
    echo "<tr><td bgcolor=\"white\">".$row['type']."</td>";
    echo "<br";
    echo "<tr><td bgcolor=\"white\">".$row['size']."</td>";
    echo "<br";
    echo "<tr><td bgcolor=\"white\">".$row['quantity']."</td>";
    echo "<br";
    echo "<tr><td bgcolor=\"white\">".$row['prize']."</td>";
    echo "<br";
	echo "<tr><td bgcolor=\"white\"><a href='update.php?id=" . $row['id'] . "'><img alt=\"Edit\"  height=\"15\" src=\"icons/edit-icon.jpg\" width=\"15\"></a><img alt=\"Delete\"  height=\"15\" src=\"icons/delete-icon.jpg\" width=\"15\"><img alt=\"Edit\"  height=\"15\" src=\"icons/view-icon.jpg\" width=\"15\"></td>";
    echo "</tr>";
    echo "<br>";

	$i++;   

}
mysql_free_result($result);
?>
<h3 style="font-family:'Arial Black'">total products: <?php echo($i-1); ?> </h3> 


 
 </table>
</html> 







in this view.php page i can only see a broken image
any response is much appreciated . thank you .

Is This A Good Question/Topic? 0
  • +

Replies To: image display stored in database

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: image display stored in database

Posted 04 March 2012 - 09:16 PM

You're wide open to SQL injection attacks. Read up on prepared statements.

Storing images in a database is not wise because it creates unnecessary overhead. Instead of linking to a path (stored in the database) you have to retrieve the data and store it in a file that you can link to.
Was This Post Helpful? 1
  • +
  • -

#3 skitband  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-March 12

Re: image display stored in database

Posted 05 March 2012 - 05:42 AM

can you tell me how to link an image path from the database??

thank you
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: image display stored in database

Posted 05 March 2012 - 06:41 AM

View PostCTphpnwb, on 05 March 2012 - 12:16 AM, said:

you have to retrieve the data and store it in a file that you can link to.

Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: image display stored in database

Posted 05 March 2012 - 07:12 AM

What CTphpnwb says is of course right, but just to address the actual problem in your current code...

This would be the problem:
echo "<tr><td bgcolor=\"white\"><img src=".$row['image']."></td>";


The image column in your database is the binary data for the image. The <img src=""> attribute is expecting a location where the image can be found, not the binary data as a string.

What you need to do is create a separate PHP script that does NOTHING other than set the proper headers so that the browser knows to expect an image, and then print the image data. There can be NOTHING other than the image data in that response, or the image will be corrupt.

Then you use that PHP script as the image src attribute where you want the image shown:
<img src="getImage.php?id=1" alt="PHP Generated Image">

Was This Post Helpful? 0
  • +
  • -

#6 skitband  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-March 12

Re: image display stored in database

Posted 05 March 2012 - 07:38 AM

thanks for that . . i appreciate your patience understanding beginners like me ty
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: image display stored in database

Posted 05 March 2012 - 08:52 AM

Of course, you'd be much better off if you didn't store the images in the database. Store the path to the image and let the OS do what it does best (and better than a database): store and retrieve files. Then you can use:
echo "<tr><td bgcolor=\"white\"><img src=".$row['image']."></td>";

because $row['image'] will not be the image but its path.

Once again, you're still open to SQL injection attacks so you should immediately learn to use prepared statements. Do not pass go, collect $200, or try to get your code "working" until you do this. We don't want you coming back here complaining that all of your data/code/financial information has been lost or worse.
Was This Post Helpful? 0
  • +
  • -

#8 skitband  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-March 12

Re: image display stored in database

Posted 05 March 2012 - 09:22 AM

THANK YOU AGAIN FOR THIS again i'll try it later and i hope i can call your help again if the i cant still get it the solution ty
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: image display stored in database

Posted 05 March 2012 - 09:41 AM

If by get it later you mean after lunch then all is well. If you mean that you intend to get it "working" first and then fix the database code then you're doing it wrong. First priority is security. Second is quality code. Third priority, whatever it may be, is something far above just getting it working. Getting it working is dead last amongst priorities. ALWAYS.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1