How to prevent duplicate recored insert into the database in php?

Step - 1 : Redirect your message or data into the other page.

Step - 2 : Redirect your message in your current page.

Let's See This Above Two Step:

Step - 1

1.Redirect your message or data into the other page.

You can redirect your message,data or information in other page like this:


CODE OF ABOVE EXPLANATION:


DATABASE TABLE STRUCTURE:
Database Name : student_information
table Name : mba_student
table column name : 
    1.srno [auto increment];
    2.fname[varchar][length 15]
    3.lname[varchar][length 15]
    4.email[varchar][length 50]
    5.cno[varchar][length 12]
    6.time[datatime]
DATABASE CONNECTION:
<?php
    $servername = "localhost";
    $username = "root";
    $pwd = "";
    $dbname = "student_information";
    $con = mysqli_connect($servername, $username, $pwd, $dbname);
    if ($con) {
        // echo 'Connection Succeessfully';
    } else {
        echo 'Check your Connection ';
    }
?> 
PHP CODE OF INSERT DATA INTO THE DATABASE:
<?php
            include('dbconnect.php');
            if ($_SERVER['REQUEST_METHOD'] == "POST") {
                $fname = $_POST['fname'];
                $lname = $_POST['lname'];
                $email = $_POST['email'];
                $cno = $_POST['cno'];
                $insert_query = "INSERT INTO `mba_student` ( `fname`, `lname`, `email`, `cno`) VALUES ( '$fname', '$lname', '$email', '$cno');";
                $result = mysqli_query($con, $insert_query);
                if ($result) {
                    header("location:home_page.php?insert='true'");
                } else {
                    echo 'Error';
                }
            }
?>
1. STUDENT INFORMATION FORM:

<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<title>Cannot insert duplicate recored into the database in php</title>
</head>

<body>
<div class="container my-5">
    <h2>Student Information Form</h2>
    <form method="POST" name="studentForm" id="studentForm">
        <div class="mb-3">
            <label for="fname" class="form-label">First Name:</label>
            <input type="text" autocomplete="off" class="form-control" id="fname" name="fname">
        </div>
        <div class="mb-3">
            <label for="lname" class="form-label">Last Name:</label>
            <input type="text" autocomplete="off" class="form-control" id="lname" name="lname">
        </div>
        <div class="mb-3">
            <label for="email" class="form-label">Email address:</label>
            <input type="email" autocomplete="off" class="form-control" id="email" aria-describedby="emailHelp"
                name="email">
            <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
        </div>
        <div class="mb-3">
            <label for="cno" class="form-label">Contact No:</label>
            <input type="number" autocomplete="off" class="form-control" id="cno" name="cno">
        </div>
        <button  type="submit" class="btn btn-primary" id="sBtn">Submit</button>
    </form>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
    crossorigin="anonymous"></script>
</body>

</html>
        
2.REDIRECT YOUR MESSAGE IN THIS HOME PAGE:
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>

<body>
<h2>home page</h2>
<?php
$insert = $_GET['insert'];
if ($insert == true) {

    echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>Success!</strong> Your Information Has Been Inserted.
<button> type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>';
}
?>
<script>> src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous">
    </script>
</body>

</html>
        

Step - 2

1.Redirect your message in your current page.

You can redirect your message,data or information in your current page like this:


CODE OF ABOVE EXPLANATION:


1.PHP CODE OF INSERT DATA INTO THE DATABASE:
<?php
include('dbconnect.php');
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $cno = $_POST['cno'];
    $insert_query = "INSERT INTO `mba_student` ( `fname`, `lname`, `email`, `cno`) VALUES ( '$fname', '$lname', '$email', '$cno');";
    $result = mysqli_query($con, $insert_query);
    if ($result) {
        echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>Success!</strong> Your Information Has Been Inserted.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>';
    } else {
        echo 'Error';
    }
}
?>

2.STUDENT INFORMATION FORM:
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<title>Cannot insert duplicate recored into the database in php</title>
</head>
<body>
<div class="container my-5">
    <h2>Student Information Form</h2>
    <form method="POST" name="studentForm" id="studentForm">
        <div class="mb-3">
            <label> for="fname" class="form-label">First Name:</label>
            <input type="text" autocomplete="off" class="form-control" id="fname" name="fname">
        </div>
        <div class="mb-3">
            <label> for="lname" class="form-label">Last Name:</label>
            <input type="text" autocomplete="off" class="form-control" id="lname" name="lname">
        </div>
        <div class="mb-3">
            <label> for="email" class="form-label">Email address:</label>
            <input type="email" autocomplete="off" class="form-control" id="email" aria-describedby="emailHelp"
                name="email">
            <div> id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
        </div>
        <div class="mb-3">
            <label> for="cno" class="form-label">Contact No:</label>
            <input type="number" autocomplete="off" class="form-control" id="cno" name="cno">
        </div>
        <button> type="submit" class="btn btn-primary" id="sBtn">Submit</button>
    </form>
</div>

<script> src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
    crossorigin="anonymous"></script>
<script> src="logic.js"></script>
</body>

</html>

3.ADD THIS JAVASCRIPT :
<script>
if (window.history.replaceState) {
    window.history.replaceState(null, null, window.history.href);
}
</script>

What is window.history.replaceState ?

The History.replaceState method modifies the current history entry replaceing it with the stateObj,title,and URL passed in the method parameters.This method is particularly useful when you want to update the state object or URL the current history entry in response to some user action.

Post a Comment

0 Comments