i have set up a mysql db for storing news articles to be displayed within a flash website. i have now been asked if i could add an image upload function to this section.

i have created the upload page and i can upload the images to the db fine. the problem that i have is that when the image is uploaded it is put into its own row in the db. i need to be able to have the code find the last entry in the db and then update this entry with the image that is uploaded.

i would like to no if there is a function within mysql that can find the last entry. i have set the table up with an id which is auto incremented. i would like to no if i can find out what the last id number is and then update the row using the id number

Recommended Answers

All 11 Replies

Member Avatar for Rhyan

You can find the last id in your table using the following select:

Select max(id) from Yourtable;

I am not sure if you can run an update using max(id) in where statement.
You can try
"Update yourtable set image=image.jpg where id=max(id);"

so if i use the select max(id) would i beable to do something like

select max(id) from interal;
$lastid=max(id);
$sql = update internal set image = $image where id = $lastid;

you can do it that way, i would suggest returning a value from a stored procedure like

select LAST_INSERT_ID();
Member Avatar for Rhyan

You should do it like this

$run=mysql_query('select max(id) from interal');
$lastid = mysql_fetch_row($run);
$lastid=$lastid[0];

Then it should be OK.

Please note that last_insert_id() would return value of the last inserted id ONLY for the current login session. It will not work if you have uploaded your news, you have closed your browser, and went on uploading the image after some time.

it dont like this line

$lastid = mysql_fetch_row($run);

i get this error

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/acmeart/public_html/flashtest/index.php on line 204

spelling mistake ignor last post

that section of code is working now it is giving me the number of the last entered row.

my update query is not working tho it is not putting the image into the db. here is my code for this section including the code you have helped with

$run=mysql_query('select max(id) from internal');
$lastid = mysql_fetch_row($run);
$lastid=$lastid[0];
$sql="UPDATE internal SET image = $consname2  WHERE id = $lastid";
$query = mysql_query($sql);

it shows the number for the last row in the db when the page is first loaded then the user enters the article information it goes to a page that tells them if it was inserted then they are redirected to the page where they entered the article. once back on this page the user then has the option of uploading an image. (the reason for it working this way is that the image upload section is only just being added)

when the user is redirected to the page the number of the last row disappears from the page. i have echoed this out for testing purposes and it shows when the page is first loaded.

it is giving you the highest id in that table, when you do the insert use

select LAST_INSERT_ID();

this will give you the id of that row, not the highest one in the table

the way it is working (i no it is not the best way to do it) is when the user uploads an image it will be to go with the last article added so th highest number in the id column will work fine in this situation.

i hate programming!!

not really it wouldnt work coz i aint put ' ' around my variable names in the update code.

When I upload pictures connected with a news id i have a field in the pictures table with

pictureid, newsid , picturetitle, picturefile, enterdate, ( ETC )

This allows a 1 to many for the news article, makes it easy to update just the one and and you can make a list of all the pictures associated with the news item.

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.