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.

Recommended Answers

All 38 Replies

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.

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

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.

commented: knows his stuff +1

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

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..

commented: very good at what he does. +1

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.

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.

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.

Yep, I guess..

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 type=file1 would the file browse button still appear?

i will check and get back

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.

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.

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 type=file1 would the file browse button still appear?

i will check and get back

No. Not that. <input type="file" name="uploadimage1"> This one.

$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.

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.

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.

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.

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'].'"/>';
        }

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

should the where clause look something like this

Help with Code Tags
(Toggle Plain Text)

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

$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

No. You are trying to update broad1 column where broad1 = 1. I hope your table has an id(counter) field which is an auto-increment field ? Use that to update the record.

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

Help with Code Tags
(Toggle Plain Text)

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

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

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

Help with Code Tags
(Toggle Plain Text)

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

Basically, If your table has only 1 record, you don't need a while loop. But if your table has more than 1 record, then to fetch all the records, you have to use a while loop. My suggestion for you is, search for "file upload+php" in google. You will get alot of examples. Use one to upload an image to the server. While you upload an image to the server, save the path in the table(Insert a record and not update). So, whenever you upload an image, a path will be in the table. And for listing the images in the preview screen, get all the records, use a while loop to fetch all the records and then display it using <img src> tag.

on the preview page the code used to display the image which is

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

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

i have changed the code so it prints out what is in the table the code now looks lie this

$query = "SELECT broad1 FROM images_broad";
$result=mysql_query($query);
print_r(mysql_fetch_array($result));

the result that it prints on the screen seems to suggest that the same image is being entered into the table twice. here is what was printed

Array ( [0] => image/thumbs/thumb_1207138801.gif
[broad1] => image/thumbs/thumb_1207138801.gif )

i have set an id counter but when i try to set the where clause to id=1 it does not like the while loop on the preview page. the path is being stored to the database fine but i think it is storing it more than once.

i need to look up the code so when the image is being displayed it only displays one row from the table and not all the rows thus taking out the while loop.

Array ( [0] => image/thumbs/thumb_1207138801.gif
[broad1] => image/thumbs/thumb_1207138801.gif )

Thats because, mysql_fetch_array returns both numeric index array and associated array name. Try mysql_fetch_array($result,MYSQL_ASSOC) and print !

And, if your table has more than 1 row, use while loop.

i have changed the code now so it is only trying to display one row from the table but i am getting an error which is as follows but do you no what this means.


Wrong parameter count for mysql_fetch_row() in /home/acmeart/public_html/mercury/broad_prev.php on line 58

the code which i am using now looks like this

$query = "SELECT * FROM images_broad WHERE id=`1`";
$result=mysql_query($query);
while($row=mysql_fetch_row($result, MYSQL_ASSOC)){
echo '<img src="'.$row['broad1'].'"/>';
}

line 58 is the while loop line

$query = "SELECT * FROM images_broad WHERE id=`1`";
$result=mysql_query($query);
while($row=mysql_fetch_row($result, MYSQL_ASSOC)){
echo '<img src="'.$row.'"/>';
}

Should be

$query = "SELECT * FROM images_broad WHERE id=`1`";
$result=mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<img src="'.$row['broad1'].'"/>';
}

that code now gives me the error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

it does not like the while loop for some reason.

Huh! Well, it likes while loop if the resource id provided to it is correct. Check your query again. Maybe some details are missing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.