Getting the Last Inserted ID in MySQL Table using PHP Script


Getting the Last Inserted ID in MySQL Table using PHP Script

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 show the id that will be inserted in our MySQL database. I’ve included 2 mysqli methods in this tutorial in the comments. So, feel free to switch between the two.

Creating our Database

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.

  1. CREATE TABLE `user` (
  2. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `firstname` VARCHAR(30) NOT NULL,
  4. `lastname` VARCHAR(30) NOT NULL,
  5. `address` VARCHAR(100) NOT NULL,
  6. PRIMARY KEY(`userid`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

 

Creating our 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 our 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>

 

 

 

Read Me

 


Have any Question or Comment?

Leave a Reply

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

Categories