亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 SQL-Server 會為單個條目保存重復數據?

為什么 SQL-Server 會為單個條目保存重復數據?

PHP
慕容3067478 2022-01-08 20:25:45
我在 php 中開發了一個基于 CRUD 的 Web 應用程序。這是一個表單,其輸入數據保存在 sql server 數據庫中。即使用戶填寫一次,數據庫也會為其保存多個條目。問題出在數據庫中,似乎表單已成功提交。任何人都可以幫忙嗎?謝謝這是代碼:<?phpob_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;    $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));
查看完整描述

1 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

調用$q->execute會觸發執行過程,即使您在邏輯檢查中使用它 - 所以它會執行兩次。您應該將其更新為:


<?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>';

    }

}

?>


查看完整回答
反對 回復 2022-01-08
  • 1 回答
  • 0 關注
  • 157 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號