基本上,我创建了一个多页表单,它使用$ _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);
}
?>
我的问题是,它没有被输入我的表格&#34; onlineformdata&#34;在数据库&#34; onlineform&#34;。确认页面有一个名为&#34;确认&#34;的按钮。包含在标签中的。如何解决此问题,以便将其存储在我的MySQL服务器中?谢谢。
答案 0 :(得分:0)
您的SQL查询编写得非常糟糕,并且有一些错误信息,正如karthikr所指出的那样。
通过在脚本中实现代码样式可以轻松避免这种情况。您可以通过执行以下操作来提高长期和复杂查询的可读性:
$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)) . ")");
变得非常容易理解发生了什么,因为信息是按照模板组织的,并且遵循模板。