PHP Forms/SQL Error: Unexpected End of File


PHP Forms/SQL Error: Unexpected End of File

我所要做的就是执行SQL查询,将数据从表单中的每个文本输入框传输到数据库。我一直收到DatabaseStuff.php的以下错误:

解析错误:语法错误,意外的文件结束/home/ar04063/public_html/DatabaseStuff.php第38行

我做错了什么?我还需要做些什么才能让它正常工作吗?下面是一些示例代码:

XYZSurvey.php:

<?php
require_once 'Form.php';
require_once 'RadioButton.php';
require_once 'TextInput.php';
require_once 'Validator.php';
$form = new Form();
$form -> addInput(new TextInput("First Name: ", "first_name"));
$form -> addInput(new TextInput("Last Name: ", "last_name"));
$form -> addInput(new TextInput("Age: ", "ice_cream_flavor"));
$radio1 = new RadioButton("Gender:", "gender");
$radio1 -> addOption("Male", "male");
$radio1 -> addOption("Female", "female");
$form -> addInput($radio1);
$form -> addInput(new TextInput("Email: ", "email_address"));
$form -> addInput(new TextInput("", "","Submit","","",false,"submit"));
echo $form -> generateHTML();

DatabaseStuff.php:

<?php
require_once 'XYZSurvey.php';
$host = "localhost";
$username = "W********";
$password = "**********";
$dbname = "W01104063";
$firstname = $_POST["first_name"];
$lastname = $_POST["last_name"];
$email = $_POST["email"];
$gender = $_POST["gender"];
try {
    $dbh = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
} catch (PDOException $e)
{
    echo "Sorry, an error occured.";
    echo $e->getMessage();
    die();
}
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$stmtSurvey = $dbh->prepare("INSERT INTO PersonalInfo (firstname, lastname, email, gender) VALUES ('$firstname', '$lastname', '$email', '$gender');
$stmtSurvey->bindValue(':firstname', 'Ashley');
$stmtSurvey->bindValue(':lastname', 'Richardson');
$stmtSurvey->bindValue(':email', 'null');
$stmtSurvey->bindValue(':gender', 'null');
$stmtSurvey->setFetchMode(PDO::FETCH_ASSOC); //Set Fetch mode to Assoc
$stmtSurvey->execute();
while($survey = $stmtSurvey->fetch())
{
    var_dump($survey);
}

DatabaseSuff.php中本行末尾缺少右引号和右括号:

$stmtSurvey = $dbh->prepare("INSERT INTO PersonalInfo (firstname, lastname, email, gender) VALUES ('$firstname', '$lastname', '$email', '$gender');

替换为:

$stmtSurvey = $dbh->prepare("INSERT INTO PersonalInfo (firstname, lastname, email, gender) VALUES ('$firstname', '$lastname', '$email', '$gender')");

希望能有所帮助。

您漏了一个右括号:

require_once 'XYZSurvey.php';
$host = "localhost";
$username = "W********";
$password = "**********";
$dbname = "W01104063";
$firstname = $_POST["first_name"];
$lastname = $_POST["last_name"];
$email = $_POST["email"];
$gender = $_POST["gender"];
try {
    $dbh = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
} catch (PDOException $e)
{
    echo "Sorry, an error occured.";
    echo $e->getMessage();
    die();
}
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
//You missed 1 closing parentheses here 
$stmtSurvey = $dbh->prepare('INSERT INTO PersonalInfo(firstname, lastname, email, gender) VALUES ("$firstname", "$lastname", "$email", "$gender")');

$stmtSurvey->bindValue(':firstname', 'Ashley');
$stmtSurvey->bindValue(':lastname', 'Richardson');
$stmtSurvey->bindValue(':email', 'null');
$stmtSurvey->bindValue(':gender', 'null');
$stmtSurvey->setFetchMode(PDO::FETCH_ASSOC); //Set Fetch mode to Assoc
$stmtSurvey->execute();
while($survey = $stmtSurvey->fetch())
{
    var_dump($survey);
}

这里少了一个右括号:

$stmtSurvey = $dbh->prepare(
        "INSERT INTO PersonalInfo (firstname, lastname, email, gender)
         VALUES ('$firstname', '$lastname', '$email', '$gender') ");
                                                                 ^ - this one