0

基本的に、$_SESSIONS を使用してユーザーが入力した値を持ち運ぶ複数ページのフォームを作成しました。私の最後のページは、データベースに入力せずにすべてのセッション値を表示する確認ページだったので、セッション変数は機能します。確認ページに続くphpページはこちら。

<?php
session_start();
if($_POST['formSubmit'] == "Confirm")
{
        $errorMessage = "";

        $varFirstName=$_SESSION['firstname'];
        $varLastName=$_SESSION['lastname'];
        $varBirthdate=$_SESSION['birthdate'];
        $varSex=$_SESSION['sex'];
        $varHealthCardPart1=$_SESSION['healthcardpart1'];
        $varHealthCardPart2=$_SESSION['healthcardpart2'];
        $varHealthCardPart3=$_SESSION['healthcardpart3'];
        $varHealthCardPart4=$_SESSION['healthcardpart4'];
        $varSpecialInterests=$_SESSION['specialInterests'];
        $varSpecialConsiderations=$_SESSION['specialConsiderations'];
        $adultFirstName1=$_SESSION['adultFirstName1'];
        $adultLastName1=$_SESSION['adultLastName1'];
        $relationship1=$_SESSION['relationship1'];
        $adultFirstName2=$_SESSION['adultFirstName2'];
        $adultLastName2=$_SESSION['adultLastName2'];
        $relationship2=$_SESSION['relationship2'];
        $familyAddress=$_SESSION['familyaddress'];
        $unit=$_SESSION['unit#'];
        $postalCode=$_SESSION['postalCode'];
        $city=$_SESSION['city'];
        $homeNumber = $_SESSION['home#'];
        $workNumber= $_SESSION['work#'];
        $emailAddress = $_SESSION['email'];
        $emergencyFirstName1 = $_SESSION['emergencyContactFirstName1']; 
        $emergencyLastName1  = $_SESSION['emergencyContactLastName1']; 
        $emergencyContactPhone1 = $_SESSION['econtactphone1']; 
        $emergencyContactRelationship1 = $_SESSION['erelationship1'];
        $emergencyFirstName2 =  $_SESSION['emergencyContactFirstName2']; 
        $emergencyLastName2 = $_SESSION['emergencyContactLastName2']; 
        $emergencyContactPhone2 = $_SESSION['econtactphone2']; 
        $emergencyContactRelationship2 = $_SESSION['erelationship2'];   
        $varPickedUp = $_SESSION['camperpickedup'];
        $varPersonPickingUp = $_SESSION['personPickingUpCamper'];
        $varTotalPrice = $_SESSION['totalPrice'];
        $campcare = $_SESSION['campcare'];
        $campsessions = $_SESSION['campsessions'];

if (empty($errorMessage))
    {
        $db= mysql_connect("localhost", "root", "root");
        if(!$db) die("Error connecting to MySQL database.");
        mysql_select_db("onlineform", $db);

        $sql = "INSERT INTO onlineformdata (firstname, lastname, birthdate, sex, healthcardpart1, healthcardpart2, healthcardpart3, healthcardpart4, specialinterests, specialconsiderations, firstNameOfAdult1, lastNameOfAdult1, adult1RelationshipToChild, firstNameOfAdult2, lastNameOfAdult2, adult2RelationshipToChild, address, apartmentNumber, postalCode, city, homeNumber, workNumber, emailAddress, emergencyContact1FirstName, emergencyContact1LastName, emergencyContact1PhoneNumber, emergencyContact1RelationshipToChild, emergencyContact2FirstName, emergencyContact2LastName, emergencyContact2PhoneNumber, emergencyContact2RelationshipToChild, isCamperPickedUp, personPickingUpCamper, specificCampSessions, needCampCare, totalPrice) VALUES (". PrepSQL($varFirstName) . ", " . PrepSQL($varLastName) . ", " . PrepSQL($varBirthdate) . "," . PrepSQL($varSex) . "," . PrepSQL($varHealthCardPart1) . "," . PrepSQL($varHealthCardPart2) . "," . PrepSQL($varHealthCardPart3) . "," . PrepSQL($varHealthCardPart4) . "," . PrepSQL($varSpecialInterests) . "," . PrepSQL($varSpecialConsiderations) . PrepSQL($adultFirstName1) . ", " . PrepSQL($adultLastName1) . ", " . PrepSQL($relationship1) . "," . PrepSQL($adultFirstName2) . "," . PrepSQL($adultLastName2) . "," . PrepSQL($relationship2) . "," . PrepSQL($familyaddress) . "," . PrepSQL($unit) . "," . PrepSQL($postalCode) . "," . PrepSQL($city) . "," . PrepSQL($homenumber) . "," . PrepSQL($worknumber) . "," . PrepSQL($emailAddress) . PrepSQL($emergencyFirstName1) . ", " . PrepSQL($emergencyLastName1) . ", " . PrepSQL($emergencyContactPhone1) . "," . PrepSQL($emergencyContactRelationship1) . "," . PrepSQL($emergencyFirstName2) . "," . PrepSQL($emergencyLastName2) . "," . PrepSQL($emergencyContactPhone2) . "," . PrepSQL($emergencyContactRelationship2) . PrepSQL($varPickedUp) . "," . PrepSQL($varPersonPickingUp) . PrepSQL($campsessions) . "," . PrepSQL($campcare) . PrepSQL($varTotalPrice) . ")";
        mysql_query($sql);

    }
}

 function PrepSQL($value)
    {
        // Stripslashes
        if(get_magic_quotes_gpc()) 
        {
            $value = stripslashes($value);
        }

        // Quote
        $value = "'" . mysql_real_escape_string($value) . "'";

        return($value);
    }

?>

そして私の問題は、データベース「onlineform」の下のテーブル「onlineformdata」に入力されていないことです。確認ページには、タグで囲まれた「確認」という名前のボタンがあります。この問題を解決して、MySQL サーバーに保存できるようにするにはどうすればよいですか? ありがとうございました。

4

1 に答える 1

0

karthikr によって指摘されたように、SQL クエリはひどく書かれており、いくつかのタイプミスがあります。

スクリプトにコード スタイルを実装することで、簡単に回避できます。次のようなことを行うことで、そのような長くて複雑なクエリの読みやすさを向上させることができます。

$data = array(
    'firstname'                            =>  $_SESSION['firstname'],
    'lastname'                             =>  $_SESSION['lastname'],
    'birthdate'                            =>  $_SESSION['birthdate'],
    'sex'                                  =>  $_SESSION['sex'],
    'healthcardpart1'                      =>  $_SESSION['healthcardpart1'],
    'healthcardpart2'                      =>  $_SESSION['healthcardpart2'],
    'healthcardpart3'                      =>  $_SESSION['healthcardpart3'],
    'healthcardpart4'                      =>  $_SESSION['healthcardpart4'],
    'specialinterests'                     =>  $_SESSION['specialInterests'],
    'specialconsiderations'                =>  $_SESSION['specialConsiderations'],
    'firstNameOfAdult1'                    =>  $_SESSION['adultFirstName1'],
    'lastNameOfAdult1'                     =>  $_SESSION['adultLastName1'],
    'adult1RelationshipToChild'            =>  $_SESSION['relationship1'],
    'firstNameOfAdult2'                    =>  $_SESSION['adultFirstName2'],
    'lastNameOfAdult2'                     =>  $_SESSION['adultLastName2'],
    'adult2RelationshipToChild'            =>  $_SESSION['relationship2'],
    'address'                              =>  $_SESSION['familyaddress'],
    'apartmentNumber'                      =>  $_SESSION['unit#'],
    'postalCode'                           =>  $_SESSION['postalCode'],
    'city'                                 =>  $_SESSION['city'],
    'homeNumber'                           =>  $_SESSION['home#'],
    'workNumber'                           =>  $_SESSION['work#'],
    'emailAddress'                         =>  $_SESSION['email'],
    'emergencyContact1FirstName'           =>  $_SESSION['emergencyContactFirstName1'],
    'emergencyContact1LastName'            =>  $_SESSION['emergencyContactLastName1'],
    'emergencyContact1PhoneNumber'         =>  $_SESSION['econtactphone1'],
    'emergencyContact1RelationshipToChild' =>  $_SESSION['erelationship1'],
    'emergencyContact2FirstName'           =>  $_SESSION['emergencyContactFirstName2'],
    'emergencyContact2LastName'            =>  $_SESSION['emergencyContactLastName2'],
    'emergencyContact2PhoneNumber'         =>  $_SESSION['econtactphone2'],
    'emergencyContact2RelationshipToChild' =>  $_SESSION['erelationship2'],
    'isCamperPickedUp'                     =>  $_SESSION['camperpickedup'],
    'personPickingUpCamper'                =>  $_SESSION['personPickingUpCamper'],
    'totalPrice'                           =>  $_SESSION['totalPrice'],
    'needCampCare'                         =>  $_SESSION['campcare'],
    'specificCampSessions'                 =>  $_SESSION['campsessions']
);

mysql_query("INSERT INTO `onlineformdata` (`" . implode("`, `", array_keys($data)) . "`) VALUES (" . implode(", ", array_map('PrepSQL', $data)) . ")");

情報が整理され、テンプレートに従っているため、何が起こっているのかを非常に理解しやすくなります。

于 2013-07-03T21:08:18.397 に答える