0

我正在做这个项目,它已经完成了,它是一个验证用户是否提交无效数据的表单,但由于某种原因,数据不会存储在表中,因此不会被发布回屏幕,我只是想知道是否有人能发现我哪里出错了,因为我已经看了一个多小时了。非常感谢。

下面是我的代码:

索引.php

<!Doctype html public>
<body>
<table cellpadding="5">
<td>
<h1> Games Club Website</h1>
<form action="process.php" method = "post"> 

 <tr>
                    <td class="label">
                        <label for="firstName">
                            First name
                        </label>
                    </td>
                    <td>
                        <input type="text"
                               name="firstName"
                               id="firstName"
                               value="<?php
                                      if (isset($validator))
                                          echo $validator->getValue('firstName');
                                      ?>"
                        />
                        <span class="error">
                            <?php
                            if (isset($validator))
                                echo $validator->getError('firstName');
                            ?>
                        </span>
                    </td>
                </tr>






                 <tr>
                    <td class="label">
                        <label for="lastName">
                           Surname
                        </label>
                    </td>
                    <td>
                        <input type="text"
                               name="lastName"
                               id="lastName"
                               value="<?php
                                      if (isset($validator))
                                          echo $validator->getValue('lastName');
                                      ?>"
                        />
                        <span class="error">
                            <?php
                            if (isset($validator))
                                echo $validator->getError('lastName');
                            ?>
                        </span>
                    </td>
                </tr>







                     <tr>
                    <td class="label">
                        <label for="email">
                           Email Address
                        </label>
                    </td>
                    <td>
                        <input type="text"
                               name="email"
                               id="email"
                               value="<?php
                                      if (isset($validator))
                                          echo $validator->getValue('email');
                                      ?>"
                        />
                        <span class="error">
                            <?php
                            if (isset($validator))
                                echo $validator->getError('email');
                            ?>
                        </span>
                    </td>
                </tr>







                     <tr>
                    <td class="label">
                        <label for="age">
                           Age
                        </label>
                    </td>
                    <td>
                        <input type="text"
                               name="age"
                               id="age"
                               value="<?php
                                      if (isset($validator))
                                          echo $validator->getValue('age');
                                      ?>"
                        />
                        <span class="error">
                            <?php
                            if (isset($validator))
                                echo $validator->getError('age');
                            ?>
                        </span>
                    </td>
                </tr>



<tr>
                    <td class="label">
                        <label>
                            Gender
                        </label>
                    </td>
                    <td>
                        <label for="genderMale">Male</label>
                        <input type="radio"
                               name="gender"
                               id="genderMale"
                               value="Male"
                               <?php
                               if (isset($validator))
                                   echo $validator->isChecked("gender", "Male");
                               ?>
                        />

                        <label for="genderFemale">Female?</label>
                        <input type="radio"
                               name="gender"
                               id="genderFemale"
                               value="Female"
                               <?php
                               if (isset($validator))
                                   echo $validator->isChecked("gender", "Female");
                               ?>
                        />
                        <span class="error">
                            <?php
                            if (isset($validator))
                                echo $validator->getError('gender');
                            ?>
                        </span>
                    </td>
                </tr>







                <tr>
                    <td class="label">
                        <label>
                            What is your preferred gaming platform?
                        </label>
                    </td>
                    <td>
                        <label for="consoleXbox">Xbox 360</label>
                        <input type="radio"
                               name="console"
                               id="consoleXbox"
                               value="Xbox 360"
                               <?php
                               if (isset($validator))
                                   echo $validator->isChecked("console", "Xbox 360");
                               ?>
                        />

                          <label for="consolePs3">Playstation 3</label>
                          <input type="radio"
                               name="console"
                               id="consolePs3"
                               value="PS3"
                               <?php
                               if (isset($validator))
                                   echo $validator->isChecked("console", "PS3");
                               ?>

                        <label for="consoleWii">Nintendo Wii</label>
                        <input type="radio"
                               name="Console"
                               id="consoleWii"
                               value="Wii"
                               <?php
                               if (isset($validator))
                                   echo $validator->isChecked("console", "Wii");
                               ?>
                        />
                        <span class="error">
                            <?php
                            if (isset($validator))
                                echo $validator->getError('console');
                            ?>
                        </span>
                    </td>
                </tr>



                 <tr>
                    <td class="label">
                        <label for="password1">
                           Enter a password:
                        </label>
                    </td>
                    <td>
                        <input type="password"
                               name="p1"
                               id="p1"
                               value="<?php
                                      if (isset($validator))
                                          echo $validator->getValue('p1');
                                      ?>"
                        />
                        <span class="error">
                            <?php
                            if (isset($validator))
                                echo $validator->getError('p1');
                            ?>
                        </span>
                    </td>
                </tr>




                 <tr>
                    <td class="label">
                        <label for="p2">
                           Confirm password:
                        </label>
                    </td>
                    <td>
                        <input type="password"
                               name="p2"
                               id="p2"
                               value="<?php
                                      if (isset($validator))
                                          echo $validator->getValue('p2');
                                      ?>"
                        />
                        <span class="error">
                            <?php
                            if (isset($validator))
                                echo $validator->getError('p2');
                            ?>
                        </span>
                    </td>
                </tr>

                <tr>
                    <td></td>
                    <td>
                        <input type="submit"
                               name="submitButton"
                               id="submitButton"
                               value="Confirm Registration" />

                        <input type="reset"
                               name="resetButton"
                               id="resetButton"
                               value="Clear Data"
                               style="margin-right: 20px;" />
                    </td>
                </tr>




</form>


</td>
</table>

</body>

</html>

进程.php

<?php

require_once "FormValidator.php";

$validator = new FormValidator();

if ($validator->validate($_POST)) {
    require 'dao.php';
}
else {
    require 'index.php';
}


?>

FormValidator.php

<?php
class FormValidator {
    private $valid;
    private $errors;
    private $data;

    public function __construct() {
        $this->valid = TRUE;
        $this->errors = array();
        $this->data = NULL;
    }

    public function validate($data) {
        $this->data = $data;

        if (empty($data['firstName'])) {
            $this->valid = FALSE;
            $this->errors['firstName'] = 'A <u>First Name</u> is required<br/>';
        }
        if (empty($data['lastName'])) {
            $this->valid = FALSE;
            $this->errors['lastName'] = 'A <u>Surname</u> is required.<br/>';
        }
        if (empty($data['p1'])) {
            $this->valid = FALSE;
            $this->errors['p1'] = 'A <u>Password</u> is required.<br/>';
        }
        if (empty($data['console'])) {
            $this->valid = FALSE;
            $this->errors['console'] = 'Please choose a <u>Console</u>.<br/>';
            }
        if (empty($data['p2'])) {
            $this->valid = FALSE;
            $this->errors['p2'] = 'Please <u>Confirm</u> password.<br/>';
        }
        if (empty($data['age'])) {
            $this->valid = FALSE;
            $this->errors['age'] = 'Please enter your <u>Age</u>.<br/>';
        }
         else if (!$this->isValidIntegerInRange($data['age'], 18, 100)) {
            $this->valid = FALSE;
            $this->errors['age'] = 'Invalid age. You also need to be at least 18 to sign up.<br/>';            
        }
        if (empty($data['email'])) {
            $this->valid = FALSE;
            $this->errors['email'] = 'Please enter a valid <u>email address</u>.<br/>';
        }
        else if (!$this->isValidEmail($data['email'])) {
            $this->valid = FALSE;
            $this->errors['email'] = 'Incorrect format (name@website.something is required)<br/>';            
        }
        if (empty($data['p2'])) {
            $this->valid = FALSE;
            $this->errors['p2'] = 'Please <u>Confirm</u> password.<br/>';
        }
        if (!empty($data['p1'])
                && !empty($data['p2'])
                && $data['p1'] !== $data['p2']) {
            $this->valid = FALSE;
            $this->errors['p2'] = 'Error, passwords <u>do not match</u> .<br/>';
        }
        if (empty($data['gender'])) {
            $this->valid = FALSE;
            $this->errors['gender'] = '<u>Please select a Gender.<u>';
        }

        return $this->valid;
    }

    public function getError($key) {
        $error = "";
        if (isset($this->errors[$key])) {
            $error = $this->errors[$key];
        }
        return $error;
    }

    public function getValue($key) {
        $value = "";
        if (isset($this->data[$key])) {
            $value = $this->data[$key];
        }
        return $value;
    }

    public function isChecked($key, $value) {
        $checked = "";
        if (isset($this->data[$key]) && $this->data[$key] === $value) {
            $checked = ' checked="checked"';
        }
        return $checked;
    }

    public function isSelected($key, $value) {
        $selected = "";
        if (isset($this->data[$key]) && $this->data[$key] === $value) {
            $selected = ' selected="selected"';
        }
        return $selected;
    }

    private function isValidEmail($email) {
        return (filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE);
    }

     protected function isValidIntegerInRange($integer, $min, $max) {
        $options = array(
            'options' => array(
                'min_range' => $min,
                'max_range' => $max,
            )
        );
        return (filter_var($integer, FILTER_VALIDATE_INT, $options) !== FALSE);
    }

}
?>

道.php

<html>
<body>
<?php

//Make connection to the database
$host = "localhost";
$username = "root";
$password = "";
$database = "my_db";
$dsn = "mysql:host=$host;dbname=$database";


TRY {
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


if (isset($_POST['submit'])) {
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $age = $_POST['age'];
    $gender = $_POST['gender'];
    $console = $_POST['console'];
    $p1 = $_POST['p1'];
    $p2 = $_POST['p2'];


    if (isset($_POST['id'])) {
        //Updates the record
        $id = $_POST['id'];

        $sql = "UPDATE userprofile2 SET"
            . "firstName=".$conn->quote($fname)
            . "lastName=".$conn->quote($lastName)
            . "email=".$conn->quote($email)
            . "age=".$conn->quote($age)
            . "gender=".$conn->quote($gender)
            . "console=".$conn->quote($console)
            . "p1=".$conn->quote($p1)
            . "p2=".$conn->quote($p2)
            . "WHERE id = ".$conn->quote($id);
        $userprofile2 = $conn->query($sql);
    } else {

        // Inserts new values into table
        $sql = "INSERT INTO userprofile2(firstName, lastName, email, age, gender, console, p1, p2"
            . " ) VALUES ("
            . $conn->quote($firstName).","
            . $conn->quote($lastName).","
            . $conn->quote($email).","
            . $conn->quote($age).","
            . $conn->quote($gender).","
            . $conn->quote($console).","
            . $conn->quote($p1).","
            . $conn->quote($p2) . ")";
        $userprofile2 = $conn->query($sql);
    }
} elseif (isset($_GET['ID'])) {

    // edit mode, allows user to change a selected parameter in the table (Not working)
    $userEditDataRows = $conn->query('SELECT * FROM userprofile2 WHERE ID ='.$conn->quote($_GET['ID']));
    if (sizeof($userEditDataRows)>0) {
       // $row = $userEditDataRows[0];
        $firstName = $row['firstName'];
        $lastName = $row['lastName'];
        $email = $row['email'];
        $age = $row['age'];
        $gender = $row['gender'];
        $console = $row['console'];
        $console = $row['p1'];
        $console = $row['p2'];
        $ID = $_GET['ID'];
    }

} else {
    //Set the empty values for fields that haven't been filled in
    $firstName = '';
    $lastName = '';
    $email = '';
    $age = '';
    $gender = '';
    $console = '';
    $p1 = '';
    $p2 = '';
    $ID = false;
}
    //construct the table
    $sql = "SELECT * FROM userprofile2";
    $userprofile2 = $conn->query($sql);
    $table = '<table>';
    $table .= '<tr>';
    $table .= '<th>  ID  </th>
               <th>  First Name  </th>
               <th>  Last Name  </th>
               <th>  Email Address  </th>
               <th>  Age  </th>
               <th>  Gender  </th>
               <th>  Console  </th>
               <th>  Password  </th>
               <th>  Password (Confirmed)  </th>';

    $table .= '</tr>';
    foreach ($userprofile2 as $userprofile2) {

        $table .= '  <tr>';
        $table .= '  <td>' . $userprofile2['id'] ." ".  '</td>';
        $table .= '  <td>' . $userprofile2['firstName'] . '</td>';
        $table .= '  <td>' . $userprofile2['lastName'] . '</td>';
        $table .= '  <td>' . $userprofile2['email'] . '</td>';
        $table .= '  <td>' . $userprofile2['age'] . '</td>';
        $table .= '  <td>' . $userprofile2['gender'] . '</td>';
        $table .= '  <td>' . $userprofile2['console'] . '</td>';
        $table .= '  <td>' . $userprofile2['p1'] . '</td>';
        $table .= '  <td>' . $userprofile2['p2'] . '</td>';
        $table .= '  </tr> ';
    }

    $table .= '</table>';

} catch (PDOException $e) {
    exit("Connection failed: " . $e->getMessage());
    //catches errors and prints them to screen
}
?>

<h2>Thank you <?php echo $_POST["firstName"]; // confirmation of a successful
 //entry ?>, your details have been stored!<br /></h2>
<u><h1>Here are the contents of your database:</h1></u>
<?php echo $table ?>

</br>

<a href="index.php">Click Here</a> to go back to the form. </br>

<html>
<body>
4

1 回答 1

0

我认为问题是$_POST['id']。它没有在任何地方定义,因此由于以下代码块,它没有机会插入:

if (isset($_POST['id'])) {

尝试$_GET['id']改用:

if (isset($_GET['id'])) {

于 2013-02-06T18:28:55.643 回答