954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

display image from databse

hello i am trying to display an image from a database which has just been uploaded by the user. when they go onto the next page the image should be displayed here.

I have been able to get it to display the path for the image that haas been stored using this code

$sql= "SELECT * FROM images_broad";
	    $query = mysql_query($sql);
	    $result = mysql_fetch_array($query) or die (mysql_error());
             echo $result ['broad1'];


but when i try to use the img tag it displays nothing.

the path has been stored in the mysql table images_broad in broad1 and the path is stored in a variable called $newname2.

when the image is meant to be displayed the source code has then img tag as empty. I no it is going to be something silly stopping it from working but it would be nice if someone could show me why this is not working.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

the path of the file is store into a variable $newname2 and then broad1 in the mysql table is set to the value of the variable.

hope this helps people understand the problem a little bit more.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

I don't see variable $newname2 getting a value.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

You have stored something like:

http://www.server.com/filelocation.png

in the database?

If so when fetching the values back from the db have you tried doing something like:

$query = "SELECT * FROM images_broad";
$result=mysql_query($query);

while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<img src="'.$row['broad1'].'"/>';
}


This should do the job! Any other problems let me know.

Regards,
Alex.

phper
Posting Whiz in Training
213 posts since Nov 2006
Reputation Points: 22
Solved Threads: 19
 

that code definitely works. but it is printing the image out 6 times going down the page.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

Do you have these files on a server so that we can check the output ? Because, I don't understand what exactly is the problem..

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

if you go to www.acmert.co.uk/mercury this is what i have been working on. not all of it is set up yet but if you login (i will give you the password in a private message) and then click on the layout button then go to broadsheet upload some images and then go to the preview page you will see what i mean about the image being repeated.

also you will see the next problem i am having. instead of it placing the both images on the page it replaces the first one.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

I checked it and this is what I find.
http://acmeart.co.uk/mercury/image/thumbs/thumb_1207128520.jpg all the images have the same path. That is why the image is being repeated. While fetching the path from the table, check if it returns the right path. Then, there are only 2 input type='file'. So whenever you upload an image and then another (using the same upload file tag) it gets replaced.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

each file upload has its own page so would this the name file matter or does this need to be different each time a new upload it completed.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

Yep, I guess..

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

the type=file part of the form does this not mean that this is a browse button i have not changed this yet but if i put[icode]type=file1[/code] would the file browse button still appear?

i will check and get back

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

no if the name file is changed then the browse button disappears. the reason for the images getting replaced must have something to do with how the mysql is displaying them.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

the problem i am getting now is that on the other pages that i have just set up it is not storing anything to the mysql tables. i have created the tables on a new php page and set them to only create if they do not exist. then when i upload the images and go to the preview page nothing is being displayed.

the code i used to enter the data into the mysql tables looks like this

$tab1name="image/".$image_name;
 	   $tab1name2="image/thumbs/thumb_".$image_name;
 	   $copied = copy($_FILES['image']['tmp_name'], $tab1name);
 	   $copied = copy($_FILES['image']['tmp_name'], $tab1name2);
 	   $sql="UPDATE images_tab SET tab1='$tab1name2'";
	   $query = mysql_query($sql);


this is just a modified version of the code that works for the broadsheet upload. all that got changed was the variable names and the names of the table and the columns.

the code i used to see if the table was being populated looks like this

$query = "select * from images_tab";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){
 echo "There is something!"; //prints if there are more than 0 records
} else {
echo "There is nothing !"; // prints if there is nothing in the table
}


and it prints out the line there is nothing.

if the code works for the images_broad table why will this not work for the new tables that have been created.

because there is nothing stored in the table before this code was run should i use the insert into query so that the table gets populated at least once and then change it to update.

the reason i want it set to update is so that the server does not begin to get filled up with images after time.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

the type=file part of the form does this not mean that this is a browse button i have not changed this yet but if i put[icode]type=file1[/code] would the file browse button still appear?

i will check and get back

No. Not that. name="uploadimage1"> This one.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
$sql="UPDATE images_tab SET tab1='$tab1name2'";

You are updating all the records to tab1name. Have a where clause and update only 1 record. Since you are updating all records, it shows the same image in the preview page.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

if the code works for the images_broad table why will this not work for the new tables that have been created.

because there is nothing stored in the table before this code was run should i use the insert into query so that the table gets populated at least once and then change it to update.

Exactly ! You have to insert a record to the table in order to update it. And again, dont update all the records, but only one record.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
You are updating all the records to tab1name. Have a where clause and update only 1 record. Since you are updating all records, it shows the same image in the preview page.

i have set my tables up so that each image that is uploaded has its own column. as it has its own column should it still need a where clause because there should only be one record in each column.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

When you upload a path, a new row is inserted and the "path" is stored in a column. But when you update the table(without a where clause), it updates all the rows and replace all the path values stored in the column with the new value. So, You should have a where clause while using update.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

should the where clause look something like this

$sql="UPDATE images_broad SET broad1='$broad1name2' WHERE broad1=`1`";


if this line is correct it is effecting the preview page. when that page is accessed is says there is a problem with the mysql line

while($row=mysql_fetch_array($result, MYSQL_ASSOC)){


should the while be taken out and the line edited so it looks like this

$result=mysql_fetch_array($result, MYSQL_ASSOC)){


all that section of code looks like this

$query = "SELECT * FROM images_broad";
	$result=mysql_query($query);

	$result=mysql_fetch_array($result, MYSQL_ASSOC)){
	echo '<img src="'.$row['broad2'].'"/>';
        }
kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

the changes made to the code on the last thread do not work it just keeps throwing up errors.

kevin wood
Posting Whiz
347 posts since Feb 2008
Reputation Points: 27
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You