1

What I want to do is I want to update the last inserted ID of the mysql database to insert my uploaded file name to that last inserted ID row under the img.

This adding name function will be running after the first insert query from another process script which will insert some other data in to the database.

  • possible duplicate of [PHP: how to get last inserted ID of a table?](http://stackoverflow.com/questions/1685860/php-how-to-get-last-inserted-id-of-a-table) – Leigh Jan 28 '13 at 08:35

5 Answers5

2

before you insert you can run

SELECT MAX(id) from myTable

and insert for that id

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • well getting the last id is processed by another php and that inserts some data before the updating part from the uplodify php so this runs after the first process. – Sadaruwan Samraweera Jan 28 '13 at 07:47
1

This query may help you,

UPDATE tablename SET fieldname = 'value' WHERE id = (SELECT id FROM tablename ORDER BY id DESC LIMIT 0, 1);
Edwin Alex
  • 5,118
  • 4
  • 28
  • 50
1
$last_id = mysql_insert_id();

$sql = "UPDATE `table_name` SET `field_name` WHERE `id`=$last_id";

Note: Put mysql_insert_id() after the insert process.

Ravi
  • 2,078
  • 13
  • 23
  • 1
    This answer is correct and should be the accepted one but the question was confusing, it said "Geting the ID before the last ID of a MySQL database". "Before the last id" would not be the one returned after executing the insert – Hanky Panky Jan 28 '13 at 08:15
  • As you said @HankyPankyㇱ this is the right one I got little confused my self when posting the question. – Sadaruwan Samraweera Jan 28 '13 at 09:23
0

Use mysql_insert_id();

ref linlk : http://php.net/manual/en/function.mysql-insert-id.php

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
0

Below query may help you to solve your issue.

$last_inserted_id = mysql_insert_id();
$query = "UPDATE table SET field_name = 'value' WHERE id = $last_inserted_id";
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32