Step 1:
Make a form page say index.php
<html> <head> </head> <body> <form name="form1" action="abc.php" method="post"> First Name :<input type="text" name="firstName"> <br> Second Name :<input type="text" name="secondName"> <input type="submit"> </form> <?php $_SESSION['token'] = 1; // flag, for not to refresh second page ?> </body> </html>
Step 2:
Make a php page on which for data is submitting say abc.php
<!DOCTYPE HTML> <html> <head> <title>abcphp</title> </head> <body> <?php if ($_SESSION['token'] = 1) { // checking flag token for refresh page // START accessing form elements $firstName = $_POST['firstName']; $secondName = $_POST['secondName']; // END accessing form elements // START database connection $db_hostname = 'localhost'; $db_database = 'databseName'; $db_username = 'userName'; $db_password = 'password'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); mysql_select_db($db_database, $db_server); if (! $db_server) die("Unable to connect to MySQL: " . mysql_error()); // END database connection // START checking record for duplication $lastRecord = "SELECT * FROM test234"; $result = mysql_query($lastRecord); if (! $result) die("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); $identify = 0; for ($j = 0; $j < $rows; ++ $j) { if ($firstName == mysql_result($result, $j, 'name1') && $secondName == mysql_result($result, $j, 'name2')) { $identify = 1; // flag of record duplicates } } // END checking record for duplication if ($identify == 1) { // checking flag echo "record already exists"; exit(); } else { $query = "INSERT INTO test234(name1,name2) VALUES ('$firstName', '$secondName')"; $result = mysql_query($query); if (! $result) die("Database access failed: " . mysql_error()); $_POST['firstName'] = ""; $_POST['secondName'] = ""; $_SESSION['token'] = 0; // so that use doesn't refresh page header('Location: newfile1.php'); // redirection to thank you page } } // MySql Query else { header('Location: index.php'); } // end MySql Query ?> </body> </html>
Step 3:
make a thank you page say newfile1.php
<!DOCTYPE HTML> <html> <head> <title>thank you</title> </head> <body> <?php echo "thank you"; ?> </body> </html>