Visitors Today: 90
Total Visits: 400220

www.codester.info

Do you have source code, articles, and tutorials to share?
Visual Basic, VB.NET, C#, PHP/MySQL, C/C++, Java/JavaScript, ASP/ASP.NET, MS Access, FoxPro, SQL
You can write your own content here.

Donation


    If the information on this site helps you and you would like to make donation to XtremeXtension.com please click the LINK below.

    If you want to support us, please make a contribution. Any amount is appreciated. To make a donation via PayPal, credit card, or e-check..

    Please click the DONATE link..

    Your contribution will make XtremeXtension.com grow in the future.

Sponsor

Calendar

PHP Get ID Of Last Inserted Data In MySQL

    Posted GraniteGear Posted On June 23, 2016

    PHP 0 Current Comments 2086 Views

    Like 1 people liked this.
    Using MySQLi And PDO To Get The Last ID Inserted Data In MySQL

    We can use this to get the ID for the last data that we inserted or upddated in the table.





    Table Structure:

    SOURCE
    CREATE TABLE `tbl_registration` (
    `tbl_registration_id` int(11) NOT NULL,
    `first_name` varchar(100) NOT NULL,
    `middle_name` varchar(100) NOT NULL,
    `last_name` varchar(100) NOT NULL,
    `email` varchar(100) NOT NULL,
    `contact_number` varchar(100) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;


    Using MySQLi (Object-Oriented):

    SOURCE
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "add_query_pdo";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    $first_name=$_POST['first_name'];
    $middle_name=$_POST['middle_name'];
    $last_name=$_POST['last_name'];
    $email=$_POST['email'];
    $contact_number=$_POST['contact_number'];

    $sql = "INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)
    VALUES ('$first_name', '$middle_name', '$last_name', '$email', '$contact_number')";

    if ($conn->query($sql) === TRUE) {
    $last_id = $conn->insert_id;
    echo "New record created successfully. Last inserted ID is: " . $last_id;
    } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
    ?>



    Using MySQLi (Procedural):

    SOURCE
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "add_query_pdo";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }

    $first_name=$_POST['first_name'];
    $middle_name=$_POST['middle_name'];
    $last_name=$_POST['last_name'];
    $email=$_POST['email'];
    $contact_number=$_POST['contact_number'];

    $sql = "INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)
    VALUES ('$first_name', '$middle_name', '$last_name', '$email', '$contact_number')";

    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);
    }

    mysqli_close($conn);
    ?>



    Using PDO (PHP Data Objects):

    SOURCE
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "add_query_pdo";

    try {
    $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);

    $first_name=$_POST['first_name'];
    $middle_name=$_POST['middle_name'];
    $last_name=$_POST['last_name'];
    $email=$_POST['email'];
    $contact_number=$_POST['contact_number'];

    $sql = "INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)
    VALUES ('$first_name', '$middle_name', '$last_name', '$email', '$contact_number')";
    // use exec() because no results are returned
    $conn->exec($sql);
    $last_id = $conn->lastInsertId();
    echo "New record created successfully. Last inserted ID is: " . $last_id;
    }
    catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

    $conn = null;
    ?>




  • PHP
  • MySQLi
  • PDO

Download

Filename
Size
Downloads
104.59Kb
154
Share With Your Friends
Share Your Thoughts With Other Users

Be the first to comment.

Leave a Reply
This small test serves as access restriction against malicious bots.