-1

While I refresh my browser the entries of the registration form goes into the Database every time i press REFRESH, Professor told me to resolve this problem with the help of LAST_INSERT_ID().

I am able to get the last_insert_id from the database but doesn't know what would I do further with that ID. Please help.. enter image description here

  • You should add your code to your question, it helps others to look at it and provide you with feedback. – Raisen Dec 08 '15 at 04:47
  • Redirect to any other page or reload this page via header after form submission. – Maha Dev Dec 08 '15 at 04:47
  • you can show success message to the user – Vigneswaran S Dec 08 '15 at 04:48
  • You're teacher wants you using `mysql_` functions? You should blow his/her mind by using an updated driver and parameterized queries. As to what your teachers intent was it is unclear to me. If you made `email` unique column you wouldn't have duplicates and/or if you checked for inserts before inserting wouldnt get duplicates.. – chris85 Dec 08 '15 at 04:53
  • hey thanks for your suggestions cheris85, but I have to resolve this issue with last_insert_id() hope you will help. – Kushmeet Singh Dec 08 '15 at 13:25

3 Answers3

0

The recommended way is to use the Post/Redirect/Get pattern. There are other ways to achieve what you desire here.

I am not sure what your professor is asking to do with the last insert id. Maybe he is referring to something like this,

    if(isset($_SESSION['last_insert_id'])){ // At the beggining    
              //redirect to a another location
    }

    // Code for insertion goes here

    $_SESSION['last_insert_id'] = $last_insert_id; // Get and store the insertion id as a  session
Community
  • 1
  • 1
Abey
  • 1,408
  • 11
  • 26
  • hey a3ey thanks for the above, but still not working while I applied your code, values can't be entered into the database.please suggest else @waiting :) – Kushmeet Singh Dec 08 '15 at 13:30
  • a3ey I have attached the snap shots, please check – Kushmeet Singh Dec 09 '15 at 13:05
  • Please show the full code, you have just shown the UI for form submission. Show what you have tried. – Abey Dec 11 '15 at 04:10
  • I am making a simple registration form in our college lab but the problem is that I have to stop duplication through Last_insert_ID(). don't have the coding right now – Kushmeet Singh Dec 11 '15 at 04:49
  • a3ey, use_last_insert_id() to stop duplication entries, while I press refresh button of my browser same values goes into database repeatedly until i releases refresh button. please help sir – Kushmeet Singh Dec 11 '15 at 04:53
  • As I said, cant help you without seeing the code. We need to know what you have tried. You need to research before submitting a question. You may start from [here](http://www.eggslab.net/creating-registration-form-with-php-and-mysqli/) – Abey Dec 11 '15 at 06:18
0

I think you are using the same page to Save the Data, If it is So, then follow the following method :

<?php
if(isset($_POST[userName]))
{
  // Put your Registration Operation Code Here

  header('Location: ./Registrationform.php');
}
?>

After DataBase Insertion it redirects to the same page. Now Refresh is made with the GET Method not by the POST Method. So, you can eliminate the duplicate entries by this way.

B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
0

As per your requirement I used the Last Inserted ID for the Validation before Inserting Records in the Database.

<?php
session_start();
if(isset($_POST[userID]))
{
  $flag = false;
  if(isset($_SESSION['last_insert_id']))
  {
     if($_SESSION['last_insert_id'] == $_POST[userID])
     {
        $flag = false;
        header('Location: ./Registrationform.php');
     }
     else
     {
        $flag = true;
        $_SESSION['last_insert_id'] = $_POST[userID];
     }
  }

  if($flag == true)
  {
     // Put your Registration Operation Code Here
  }
}
?>
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130