Mysql/php update last row then insert a new one?

Soldato
Joined
7 Jan 2007
Posts
10,607
Location
Sussex, UK
Hi,

I have created a database and I need to update the last row of each foreign key, then after this insert a new row with some of the contents copied down and some new ones inserted.

Anyone able to guide me?

PHP:
id  | title | menu_id   
1    'foo'      1   
2    'bar'      1   
3    'baz'      2
 
Last edited:
Are you using MySQL Workbench or the Command Line Client?

If you can download the mySQL workbench, which is basically a UI for MySQL it makes it much easier editing tables, and has a help menu which will explain how to do it..
 
Something like:

Code:
SELECT * FROM `table` ORDER DESC LIMIT 1 //Get last row
INSERT INTO `table` (id, title, menu_id) VALUES ('$stuff', '$stuff2', '$stuff3') //Add new row

edit: I'm bored so it will be this in PHP to do what you want to do

PHP:
<?
//Get Last Row
$result = mysql_query("SELECT id FROM `table` ORDER DESC LIMIT 1'");
while($row = mysql_fetch_array($result))
		{
		$lastId = $row['id']; //Last Row Id
		}
//Update Last
mysql_query("UPDATE `table` SET title='whatever' WHERE id='$lastId'");
//Add New Row
mysql_query("INSERT INTO `table` (id, title, menu_id) VALUES ('$stuff', '$stuff2', '$stuff3')");
?>

Could be improved and I haven't tested it, but that's the kind of thing you need. If that's not what you need then give more information and I'll try to help :)
 
Last edited:
Back
Top Bottom