MySQL SQL_INSERT_ID() and LAST_INSERT_ID do not work

Tony Rowlett

I am using MySQL/PHP/Apache on a recent MacBook Pro, with all requests to localhost. I have searched but could not find a solution to my issue.

My PHP script successfully inserts a record into a basic main table whose primary key is named ID and is an Auto-Increment field, but it fails to capture the value of that ID for use with another INSERT into a different table when I try using LAST_INSERT_ID() or SQL_INSERT_ID(). The data needed for the second table need only that value and the contents of a text field from the same HTML form.

It is unmistakable that if I place an integer after the equals sign (as a test), my script works flawlessly (i.e. no error, and the script enters that required data into the second table perfectly). I have tried SQL_INSERT_ID() and LAST_INSERT_ID() with the same results: a completely white screen.

There are no foreign key constraints or anything complicated. This code fails and returns just a white screen:

$sql = "INSERT INTO Table1 
    (int1, str1, int2, int3, int4, int5) 
    VALUES (?, ?, ?, ?, ?, ?)";

if($stmt = mysqli_prepare($link, $sql)){
    mysqli_stmt_bind_param($stmt, "isiiii", 
        $param_int1, $param_str1, $param_int2, 
        $param_int3, $param_int4, $param_int5);
}

if(mysqli_stmt_execute($stmt)){
    $last_id = sql_insert_id();        // <--- problem statement
} else {
    echo "Something went wrong with the execution.";
}

But the code works exactly as I want it to when I use an integer in place of the function in question:

if(mysqli_stmt_execute($stmt)){
    $last_id = 6;
} else {
    echo "Something went wrong with the execution.";
}

The if(mysql_stmt_execute($stmt)) returns true if the first insert is successful, so it seems logical to initialize the variable $last_id right after that. Is this OK? Where else can I look in my code? Do I need to provide data inside the parentheses of the function?

MySQL version is 5.7.21 Mac OS X version 10.13.4

I consider myself an advancing beginner with PHP/MySQL. Any help would be greatly appreciated.

ArtisticPhoenix

Try

mysqli_insert_id($link);

I think you are confusing the OOP and the procedural parts of the library or something.

Because if I remember right sql_insert_id is from the now defunct mysql_* library.

https://www.w3schools.com/php/func_mysqli_insert_id.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 do a multiple insert using LAST_INSERT_ID() MySQL?

From Dev

How to do a multiple insert using LAST_INSERT_ID() MySQL?

From Dev

MYSQL last_insert_id() and concurrency

From Dev

LAST_INSERT_ID after restarting MySQL

From Dev

MYSQL last_insert_id() and concurrency

From Dev

MYSQL LAST_INSERT_ID not working as intended. How do I fix?

From Dev

MySQL LAST_INSERT_ID() - how does it works

From Dev

Thread safety of MySQL's Select Last_Insert_ID

From Dev

MySQL : Stored procedure returns null for last_insert_id

From Dev

MySQL retrieve last_insert_id from stored procedure

From Dev

MySQL: Can't get LAST_INSERT_ID()

From Dev

MySQL Function with LAST_INSERT_ID() giving unexpected results

From Dev

SQL, select last insert ID by LAST_INSERT_ID()

From Dev

SQL Add a prefix to LAST_INSERT_ID()

From Dev

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

From Dev

How do I run transactions in CakePHP3 while retrieving last insert id and work for both PostgreSQL and MySQL?

From Dev

how to select last_insert_id?

From Dev

How efficient is Last_insert_id?

From Dev

SELECT LAST_INSERT_ID() not generating correctly

From Dev

SELECT LAST_INSERT_ID() returning zero

From Dev

use of where cluase for last_insert_id

From Dev

Java Spring JDBC last_insert_id()

From Dev

MariaDB Columnstore LAST_INSERT_ID() alternative

From Dev

Mysqli last insert id not work

From Dev

Invalid descriptor index on LAST_INSERT_ID after insert

From Dev

Both sql LAST_INSERT_ID() and PHP insert_id return 0

From Dev

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

From Dev

how to get LAST_INSERT_ID via stored procedure in php

From Dev

Using select last_insert_id() from php not working

Related Related

  1. 1

    How to do a multiple insert using LAST_INSERT_ID() MySQL?

  2. 2

    How to do a multiple insert using LAST_INSERT_ID() MySQL?

  3. 3

    MYSQL last_insert_id() and concurrency

  4. 4

    LAST_INSERT_ID after restarting MySQL

  5. 5

    MYSQL last_insert_id() and concurrency

  6. 6

    MYSQL LAST_INSERT_ID not working as intended. How do I fix?

  7. 7

    MySQL LAST_INSERT_ID() - how does it works

  8. 8

    Thread safety of MySQL's Select Last_Insert_ID

  9. 9

    MySQL : Stored procedure returns null for last_insert_id

  10. 10

    MySQL retrieve last_insert_id from stored procedure

  11. 11

    MySQL: Can't get LAST_INSERT_ID()

  12. 12

    MySQL Function with LAST_INSERT_ID() giving unexpected results

  13. 13

    SQL, select last insert ID by LAST_INSERT_ID()

  14. 14

    SQL Add a prefix to LAST_INSERT_ID()

  15. 15

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

  16. 16

    How do I run transactions in CakePHP3 while retrieving last insert id and work for both PostgreSQL and MySQL?

  17. 17

    how to select last_insert_id?

  18. 18

    How efficient is Last_insert_id?

  19. 19

    SELECT LAST_INSERT_ID() not generating correctly

  20. 20

    SELECT LAST_INSERT_ID() returning zero

  21. 21

    use of where cluase for last_insert_id

  22. 22

    Java Spring JDBC last_insert_id()

  23. 23

    MariaDB Columnstore LAST_INSERT_ID() alternative

  24. 24

    Mysqli last insert id not work

  25. 25

    Invalid descriptor index on LAST_INSERT_ID after insert

  26. 26

    Both sql LAST_INSERT_ID() and PHP insert_id return 0

  27. 27

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

  28. 28

    how to get LAST_INSERT_ID via stored procedure in php

  29. 29

    Using select last_insert_id() from php not working

HotTag

Archive