Get ID from last insert into table

JohnyChew

Im inserting into multiple tables , and need to get the ID from the last insert into table1 and use that as a variable in the insert for table 2.

The IDs is auto incremented.

The query's will be run once a submit button has been clicked in a form.

Query's:

$sql = "INSERT INTO table1 (Text) VALUES ($T1text);";
$sql = "INSERT INTO table2 (table1ID,Text) VALUES ($table1id, $T2text);";


table1 {id,Text}
table2 {id,table1.id,Text}
Kerel

There is a PHP function for that (Decprecated, Removed in 7.0 !) Or the according mysql/mysqli/pdo functions.

Solutions

PHP (Deprecated)

Example

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

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

Mysql/Mysqli/PDO

Mysqli $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";

if (mysqli_query($conn, $sql)) {
    $last_id = mysqli_insert_id($conn);
    echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

PDO

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";
// use exec() because no results are returned
$conn->exec($sql);
$last_id = $conn->lastInsertId();

Source: http://www.w3schools.com/php/php_mysql_insert_lastid.asp

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the last row Id after insert data into table for SQLite

From Dev

Is it possible to get LAST_INSERT_ID() from different database?

From Dev

C# Insert MDB Database Get Last Insert ID

From Dev

Get last insert id of Postgresql

From Dev

how to get the last insert ID in linq

From Dev

MySQL: Get last update id from a table to insert it in an other table

From Dev

How to get last insert id with Phalcon?

From Dev

Get the new record primary key ID from SQL insert query to insert same value into another table?

From Dev

How to get last inserted id from insert method laravel 5.1

From Dev

Get last insert id return null

From Dev

Get Id from a conditional INSERT

From Dev

Nodjs sequalize how to get last insert id

From Dev

mysql - insert in two tables using autogenerate key from first insert table without using LAST_INSERT_ID

From Dev

Get the last entered id from a table in sql

From Dev

mysql, java - Get last inserted auto increment id from one table and insert it again on another table

From Dev

get last entered value insert from table in trigger for non auto incremental primary key

From Dev

C# Insert MDB Database Get Last Insert ID

From Dev

how to get the last insert ID in linq

From Dev

Get last inserted ID from mysql table

From Dev

MySQL: Get last update id from a table to insert it in an other table

From Dev

How to Get last Insert Id in php without insert any data?

From Dev

How to get last auto increment id from a table in phpMyAdmin

From Dev

How to get last inserted ID after insert to MySQL table with multi col primary key

From Dev

how to get the last insert id from a table in DB2 using nodejs and ibm_db package

From Dev

how to get last insert id after insert query in codeigniter

From Dev

How to get the ID of the last insert in MVC?

From Dev

Can't get last_insert_id from database with Eloquent

From Dev

Last insert id of a database table by insert_id() not Return to Controller file from Model in Codeigniter

From Dev

How to get last insert id from mysql database in php?

Related Related

  1. 1

    How to get the last row Id after insert data into table for SQLite

  2. 2

    Is it possible to get LAST_INSERT_ID() from different database?

  3. 3

    C# Insert MDB Database Get Last Insert ID

  4. 4

    Get last insert id of Postgresql

  5. 5

    how to get the last insert ID in linq

  6. 6

    MySQL: Get last update id from a table to insert it in an other table

  7. 7

    How to get last insert id with Phalcon?

  8. 8

    Get the new record primary key ID from SQL insert query to insert same value into another table?

  9. 9

    How to get last inserted id from insert method laravel 5.1

  10. 10

    Get last insert id return null

  11. 11

    Get Id from a conditional INSERT

  12. 12

    Nodjs sequalize how to get last insert id

  13. 13

    mysql - insert in two tables using autogenerate key from first insert table without using LAST_INSERT_ID

  14. 14

    Get the last entered id from a table in sql

  15. 15

    mysql, java - Get last inserted auto increment id from one table and insert it again on another table

  16. 16

    get last entered value insert from table in trigger for non auto incremental primary key

  17. 17

    C# Insert MDB Database Get Last Insert ID

  18. 18

    how to get the last insert ID in linq

  19. 19

    Get last inserted ID from mysql table

  20. 20

    MySQL: Get last update id from a table to insert it in an other table

  21. 21

    How to Get last Insert Id in php without insert any data?

  22. 22

    How to get last auto increment id from a table in phpMyAdmin

  23. 23

    How to get last inserted ID after insert to MySQL table with multi col primary key

  24. 24

    how to get the last insert id from a table in DB2 using nodejs and ibm_db package

  25. 25

    how to get last insert id after insert query in codeigniter

  26. 26

    How to get the ID of the last insert in MVC?

  27. 27

    Can't get last_insert_id from database with Eloquent

  28. 28

    Last insert id of a database table by insert_id() not Return to Controller file from Model in Codeigniter

  29. 29

    How to get last insert id from mysql database in php?

HotTag

Archive