0

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.

3
Contributors
38
Replies
39
Views
9 Years
Discussion Span
Last Post by nav33n
Featured Replies
  • You have stored something like: [url]http://www.server.com/filelocation.png[/url] in the database? If so when fetching the values back from the db have you tried doing something like: [code=php]$query = "SELECT * FROM images_broad"; $result=mysql_query($query); while($row=mysql_fetch_array($result, MYSQL_ASSOC)){ echo '<img src="'.$row['broad1'].'"/>'; }[/code] This should do the job! Any other problems let me know. Regards, … Read More

  • 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.. Read More

  • 1

    Then you can truncate the table instead of dropping it and creating a new one ! mysql_query("Truncate table tablename"); will do the trick ! Read More

0

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.

1

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.

Votes + Comments
knows his stuff
0

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

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

Votes + Comments
very good at what he does.
0

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.

0

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.

0

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

Edited by happygeek: fixed formatting

0

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.

0

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.

0

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.

Edited by happygeek: fixed formatting

0

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

0

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.

0

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.

0

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.

0

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

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

0

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.

0

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 )

0

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.

0

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 !

0

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

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

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.