Search code examples
phpmysqllastinsertid

Display recent data after button is clicked(different page)


I have a modal that adds a project to database.

When I clicked the add button I want the recent project data to appear on a pop-up window after the modal has been closed.

I think it is all about the LAST_INSERT_ID() function but it does not work.

HELP!

Function of my add button:

<?php
if (!empty(isset($_POST["submit_n"]))) {

    $project_name   = $_POST["project_name"];
    $sql = "INSERT INTO tbl_ongoing_project(project_name) VALUES('$project_name')";
    $res = $con->query($sql);
    header("Location: view_result.php");
}
?>

I wanted the header to be like this :view_result.php?id=$id $id = last insert id ???


Solution

  • You can write like this,

     $res = $con->query($sql);
     header("Location: view_result.php?id=".$con->insert_id);
    

    It should work, and as a suggestion please use mysqli.