Displaying record from database table after submit form

When I submitting html from by clicking submit button, then its displaying Post values as per below code. But my requirement is that when I submit form after clicking submit button, then it should display record (which submited recently by clicking submit ) from mysql database rather than post value.


// getting all values from the HTML form
        $mrno = $_POST['mrno'];
	 $name1 = $_POST['name1'];
        $age1 = $_POST['age1'];
        $mobno = $_POST['mobno'];
        $gender1 = $_POST['gender1']; 

    // using sql to create a data entry query
$sqlInsert = "INSERT INTO iap3 (mrno, name1, age1, mobno, gender1)
 VALUES ('$mrno', '$name1', '$age1', '$mobno', '$gender1')

 $rs = mysqli_query($con, $sqlInsert);

// Displaying Value after submit form
<table class="center" border=0  width=600>
<tr><td><b>Mrno : </td><td><?php echo $_POST["mrno"]; ?>
<tr><td><b>Name : </td><td><?php echo $_POST["name1"]; ?>
<tr><td><b>Age : </td><td><?php echo $_POST["age1"]; ?>
<tr><td><b>Mob No : </td><td><?php echo $_POST["mobno"]; ?>
<tr><td><b>Gender : </td><td><?php echo $_POST["gender1"]; ?>

So, you want your code to store the $_POST information into the database, then retrieve that same row from the database and display that information?

Does your database table have a unique identifier column, perhaps an auto-increment id field? If it does, use the appropriate mysqli function to retrieve the last insert id (I don’t know what it is, sorry, I use PDO and it’ll be different), then run a second query to retrieve the row, then display the information.

If you don’t have a unique id field in your table, how will you be able to retrieve the exact same row you just inserted? Is there anything else that has a unique constraint on it? I’m presuming not as your code doesn’t do anything to handle that situation.

You need to look at using Prepared Statements instead of putting user-supplied data directly into your database table.

what if I used following instead of last insert id ? will it do the same purpose without any drawback ?


What if two people insert a new row one after the other, or almost at the same time? Let’s say they press the “submit” button together, but of course the server only runs one task at a time, so one will run before the other. You’ll get the most recently inserted row, regardless of who inserted it.

I believe that the Last Insert ID will give the id from your current database connection. so in that scenario the two users will get different values, it will be “their” last inserted ID. You can check that in the doc, or by trying it.

ok sir! that’s what i want to knew the actual difference between these two.
if i am using last insert id, then i have to write as follow , plz correct me if i am wrong
$sqlInsert = "INSERT INTO iap3 (mrno, name1, age1, mobno, gender1)
VALUES (‘$mrno’, ‘$name1’, ‘$age1’, ‘$mobno’, ‘$gender1’)
$rs = mysqli_query($con, $sqlInsert);
$last_id = mysqli_insert_id($con);
and then for displaying i wil write another sql querry and in where clause i will mentioned $last_id
am right ??

That sounds about right, but as I said earlier, I don’t use mysqli so you will need to check the documentation to make sure that’s the correct usage. It looks OK to me, but I use PDO so any time I look at mysqli stuff I’m not certain.

After successfully POSTing a form you will almost always want to redirect to a GET request. Otherwise the back button on the browser will POST your stuff again causing all sorts of fun.

You will want to either stash $last_id in your session or have a url like /show_item?id=$last_id.

i restricted back button with java script code. by using below code
(function (global) {

if(typeof (global) === "undefined") {
    throw new Error("window is undefined");

var _hash = "!";
var noBackPlease = function () {
    global.location.href += "#";

    // Making sure we have the fruit available for juice (^__^)
    global.setTimeout(function () {
        global.location.href += "!";
    }, 50);

global.onhashchange = function () {
    if (global.location.hash !== _hash) {
        global.location.hash = _hash;

global.onload = function () {

    // Disables backspace on page except on input fields and textarea..
    document.body.onkeydown = function (e) {
        var elm = e.target.nodeName.toLowerCase();
        if (e.which === 8 && (elm !== 'input' && elm  !== 'textarea')) {
        // Stopping the event bubbling up the DOM tree...


Is it that you really want to see THAT record or see an updated LIST of records with the new item added? When I add something I almost always write a message that can be displayed to the user then reload the page in a few seconds.

$message = "Record Added";
header("refresh: 2; URL=mypage.php");

As the page reloads, the list query will be populated with new records. I rarely use GET for viewing records but as ahundiak pointed out you could save the ID to session or send the value with GET.

I suppose it really depends on the flow of your project. So if for example if you first want to add something (client, business, patient etc) then directly work with that item (client, business, patient etc) adding further details or setting up appointments etc, then I would set that items ID to session and use

header("location: mypage.php");

On the page you would have 2 main IF conditions based on if this ID is set to session so (using patient_id as an example) you either are adding a patient if(empty($_SESSION['patient_id'])) or working with an patient if(!empty($_SESSION['patient_id'])) .

1 Like

That’s great as long as none of your users have Javascript disabled, if that’s even possible these days.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.