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

Your SQL query is awfully written and has several typos as it was pointed out by karthikr.

It could be easily avoided by implementing code styles to your script. You can improve the readability of long and complex queries like that by doing something like this:

$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)) . ")");

It becomes very easy to understand what is going on because the information is organized and follows a template.

于 2013-07-03T21:08:18.397 回答