Sadaruwan Samraweera
Sadaruwan Samraweera

Reputation: 65

Updating the last inserted ID of a MySQL database

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.

Upvotes: 1

Views: 3690

Answers (5)

Devang Rathod
Devang Rathod

Reputation: 6736

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";

Upvotes: 0

Ravi
Ravi

Reputation: 2086

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

Upvotes: 1

Prasanth Bendra
Prasanth Bendra

Reputation: 32710

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());
?>

Upvotes: 0

Edwin Alex
Edwin Alex

Reputation: 5108

This query may help you,

UPDATE tablename SET fieldname = 'value' WHERE id = (SELECT id FROM tablename ORDER BY id DESC LIMIT 0, 1);

Upvotes: 1

Hanky Panky
Hanky Panky

Reputation: 46900

before you insert you can run

SELECT MAX(id) from myTable

and insert for that id

Upvotes: 2

Related Questions