Get the Last Inserted ID in MySQL Table using PHP

February 12, 2021
PHP
Get the Last Inserted ID in MySQL Table using PHP

Tutorial: How to Get the Last Inserted ID in MySQL Table using PHP with Source Code

Getting Started on How to Get the Last Inserted ID in MySQL Table using PHP

This is a tutorial on Get the Last Inserted ID in MySQL Table using PHP. In this tutorial, I’m going to show you how to get the last inserted id in our database table. In this tutorial, I have set up an Insert Form to display the identifier that will be added in our MySQL database. I’ve included two mysqli methods in this tutorial in the comments. So, feel free to switch between the two.

Creating our Database on Get the Last Inserted ID in MySQL Table using PHP

First, we’re going to create a database that contains our data.

1. Open phpMyAdmin.

2. Click databases, create a database and name it as “last_id”.

3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

CREATE TABLE `user` (
  `userid` INT(11) NOT NULL AUTO_INCREMENT,
  `firstname` VARCHAR(30) NOT NULL,
  `lastname` VARCHAR(30) NOT NULL,
  `address` VARCHAR(100) NOT NULL,
PRIMARY KEY(`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Get the Last Inserted ID in MySQL Table using PHP

Creating a Connection

Next step is to create a database connection and save it as “conn.php”. This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.

<?php
 
//MySQLi Procedural
//$conn = mysqli_connect("localhost","root","","last_id");
//if (!$conn) {
//die("Connection failed: " . mysqli_connect_error());
//}
 
//MySQLi Object-oriented
$conn = new mysqli("localhost","root","","last_id");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
 
?>

Creating a Form and Show id Script

Lastly, we create the add form as well as our show id script. To create the form, open your HTML code editor and paste the code below after the tag.

<!DOCTYPE html>
<html>
<head>
<title>Getting the Last Inserted ID in MySQL Table using PHP/MySQli</title>
</head>
<body>
    <h2>Add Form:</h2>
    <form method="POST">
    <label>Firstname:</label><input type="text" name="firstname">
    <label>Lastname:</label><input type="text" name="lastname">
    <label>Address:</label><input type="text" name="address">
    <input type="submit" value="Add" name="submit">
    </form>
    <?php
        $id="";
        $row['firstname']="";
        $row['lastname']="";
        $row['address']="";
 
        if (isset($_POST['submit'])){
 
            $firstname=$_POST['firstname'];
            $lastname=$_POST['lastname'];
            $address=$_POST['address'];
 
            include('conn.php');
 
            //MySQLi Procedural
            //mysqli_query($conn,"insert into `user` (firstname, lastname, address) values ('$firstname','$lastname','$address')");
            //$id = mysqli_insert_id($conn);
            //$query=mysqli_query($conn,"select * from `user` where userid='$id'");
            //$row=mysqli_fetch_assoc($query);
 
            //MySQLi Object-oriented
            $conn->query("insert into `user` (firstname, lastname, address) values ('$firstname','$lastname','$address')"); 
            $id = $conn->insert_id;
            $query=$conn->query("select * from `user` where userid='$id'");
            $row = $query->fetch_assoc();
        }
    ?>
    <h2>Last inserted Id: <?php echo $id; ?></h2>
    <h2>Data:</h2>
    <label>Firstname: </label> <?php echo $row['firstname']; ?><br>
    <label>Lastname: </label> <?php echo $row['lastname']; ?><br>
    <label>Address: </label> <?php echo $row['address']; ?>
</body>
</html>

Happy Coding!


Related Tutorials: Insert Array Data Into Database Using PDO in PHP, How to Create Session in PHP, Load More Data using AngularJS with PHP/MySQLi

Download Here

Leave a Reply

Your email address will not be published. Required fields are marked *