Skip to content
Advertisement

Why does SQL-Server save duplicate data for a single entry?

I’ve developed a CRUD based web application in php. It’s a form whose input data gets saved in a sql server database.

Even when the user fills it once, the database is saving multiple entries for it.

The problem is in the database it seems as the form gets submitted successfully.

Can anyone help?

Thanks

Here’s the code:

<?php
ob_start();
require_once 'db-connect.php';
require_once 'email.php';        

if(isset($_POST['pEmail'])){
    $fName = filter_input(INPUT_POST, "fName") ? filter_input(INPUT_POST, 'fName') : null;
    $lName = filter_input(INPUT_POST, "lName")? filter_input(INPUT_POST, 'lName') : null;
    $mName = filter_input(INPUT_POST, "mName")? filter_input(INPUT_POST, 'mName') : null;
    $tempRace = filter_input(INPUT_POST, "race", FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
    $race = (is_array($tempRace)) ? implode(',', $tempRace) : null;
    $hEthnicity = filter_input(INPUT_POST, "hEthnicity") ? filter_input(INPUT_POST, 'hEthnicity') : null;
    $gender = filter_input(INPUT_POST, "gender") ? filter_input(INPUT_POST, 'gender') : null;
    $age = filter_input(INPUT_POST, "age") ? filter_input(INPUT_POST, 'age') : null;
    $education = filter_input(INPUT_POST, "education") ? filter_input(INPUT_POST, 'education') : null;
    $gpa = filter_input(INPUT_POST, "gpa") ? filter_input(INPUT_POST, 'gpa') : null;
    $hPhone = filter_input(INPUT_POST, "hPhone") ? filter_input(INPUT_POST, 'hPhone') : null;
    $cPhone = filter_input(INPUT_POST, "cPhone") ? filter_input(INPUT_POST, 'cPhone') : null;
    $pEmail = filter_input(INPUT_POST, "pEmail") ? filter_input(INPUT_POST, 'pEmail') : null;
    $aEmail = filter_input(INPUT_POST, "aEmail") ? filter_input(INPUT_POST, 'aEmail') : null;
    $inputAddress = filter_input(INPUT_POST, "inputAddress") ? filter_input(INPUT_POST, 'inputAddress') : null;
    $inputCity = filter_input(INPUT_POST, "inputCity") ? filter_input(INPUT_POST, 'inputCity') : null;

    $conn = DB::databaseConnection();
    $conn->beginTransaction();
    $sql = "INSERT INTO dbo.premedical ( FName,LName, MidInitial, Race, Ethnicity, Gender, Age, SchoolYear, Gpa, HPhone, CPhone, PEmail, AEmail, MailAddress, MailCity) VALUES 
            ( :fName,:lName, :mName, :race, :hEthnicity, :gender,:age, :education, :gpa, :hPhone, :cPhone, :pEmail, :aEmail, :inputAddress, :inputCity)";

    $q = $conn->prepare($sql);
    $q->execute(array($fName,  $lName, $mName, $race, $hEthnicity, $gender, $age,   $education, $gpa, $hPhone, $cPhone, $pEmail, $aEmail, $inputAddress, $inputCity));

    if ($q->execute()) {
        $conn->commit(); 

        if (Form::mailer($fName,  $lName, $mName, $race, $hEthnicity, $gender, $age,   $education, $gpa, $hPhone, $cPhone, $pEmail, $aEmail, $inputAddress, $inputCity)) {
            echo '
            <script >
                alert("Thank you for registration.");
            </script>';
        }

        return true; 
    } else {
        echo '
        <script>
            alert("Error, please try submitting again. Error code 1");
            window.history.back();
        </script>';
    }
}
?>

Advertisement

Answer

Calling $q->execute fires off the execute procedure, even if you’re using it in a logic check – so it’s executing twice. You should update it to something like:

<?php
ob_start();
require_once 'db-connect.php';
require_once 'email.php';        

if(isset($_POST['pEmail'])){
    $fName = filter_input(INPUT_POST, "fName") ? filter_input(INPUT_POST, 'fName') : null;
    $lName = filter_input(INPUT_POST, "lName")? filter_input(INPUT_POST, 'lName') : null;
    $mName = filter_input(INPUT_POST, "mName")? filter_input(INPUT_POST, 'mName') : null;
    $tempRace = filter_input(INPUT_POST, "race", FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
    $race = (is_array($tempRace)) ? implode(',', $tempRace) : null;
    $hEthnicity = filter_input(INPUT_POST, "hEthnicity") ? filter_input(INPUT_POST, 'hEthnicity') : null;
    $gender = filter_input(INPUT_POST, "gender") ? filter_input(INPUT_POST, 'gender') : null;
    $age = filter_input(INPUT_POST, "age") ? filter_input(INPUT_POST, 'age') : null;
    $education = filter_input(INPUT_POST, "education") ? filter_input(INPUT_POST, 'education') : null;
    $gpa = filter_input(INPUT_POST, "gpa") ? filter_input(INPUT_POST, 'gpa') : null;
    $hPhone = filter_input(INPUT_POST, "hPhone") ? filter_input(INPUT_POST, 'hPhone') : null;
    $cPhone = filter_input(INPUT_POST, "cPhone") ? filter_input(INPUT_POST, 'cPhone') : null;
    $pEmail = filter_input(INPUT_POST, "pEmail") ? filter_input(INPUT_POST, 'pEmail') : null;
    $aEmail = filter_input(INPUT_POST, "aEmail") ? filter_input(INPUT_POST, 'aEmail') : null;
    $inputAddress = filter_input(INPUT_POST, "inputAddress") ? filter_input(INPUT_POST, 'inputAddress') : null;
    $inputCity = filter_input(INPUT_POST, "inputCity") ? filter_input(INPUT_POST, 'inputCity') : null;

    $conn = DB::databaseConnection();
    $conn->beginTransaction();
    $sql = "INSERT INTO dbo.premedical ( FName,LName, MidInitial, Race, Ethnicity, Gender, Age, SchoolYear, Gpa, HPhone, CPhone, PEmail, AEmail, MailAddress, MailCity) VALUES 
            ( :fName,:lName, :mName, :race, :hEthnicity, :gender,:age, :education, :gpa, :hPhone, :cPhone, :pEmail, :aEmail, :inputAddress, :inputCity)";

    $q = $conn->prepare($sql);
    $result = $q->execute(array($fName,  $lName, $mName, $race, $hEthnicity, $gender, $age,   $education, $gpa, $hPhone, $cPhone, $pEmail, $aEmail, $inputAddress, $inputCity));

    if ($result) {
        $conn->commit(); 

        if (Form::mailer($fName,  $lName, $mName, $race, $hEthnicity, $gender, $age,   $education, $gpa, $hPhone, $cPhone, $pEmail, $aEmail, $inputAddress, $inputCity)) {
            echo '
            <script >
                alert("Thank you for registration.");
            </script>';
        }

        return true; 
    } else {
        echo '
        <script>
            alert("Error, please try submitting again. Error code 1");
            window.history.back();
        </script>';
    }
}
?>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement