0

検証が完了するまで気づきませんでしたが、フォームボックスの上部にエラーが表示された場合でも、phpmyadminに移動してデータを確認し、意図的にエラーを追加してもフォームが送信されることに気付きました。 。

次に、上記の問題を含む2番目の問題は、学生IDまたは「anum」が何をしても投稿されません。データベースのstudentsテーブルに「0」の値が表示され続けます。

これはコード全体です:

<?php
//Starting session
session_start();

// Validation starts here
if (empty($_POST) === false) {
    $errors   = array();
    $anum     = $_POST['anum'];
    $first    = $_POST['first'];
    $last     = $_POST['last'];
    $why      = $_POST['why'];
    $comments = $_POST['comments'];

    if (empty($anum) === true || empty($first) === true || empty($last) === true) {
        $errors[] = 'Form is incomplete please revise it!';
    } else {

        if (ctype_alnum($anum) === false) {
            $errors[] = 'A number can only consist of alphanumeric characters!';
        }
        if ((strlen($anum) < 9) && (strlen($anum)) > 9) {
            $errors[] = 'A number is incorrect!';
        }
        if (ctype_alpha($first) === false) {
            $errors[] = 'First mame must only contain alphabetical characters!';
        }
        if (ctype_alpha($last) === false) {
            $errors[] = 'Last name must only contain alphabetical characters!';
        }
        if (empty($why))
            $errors[] = 'Please make sure to select the proper reasoning for your vistit today!';

        elseif ($why === 'Other') {

            if (empty($comments))
                $errors[] = 'Please explain the nature of your visit in the comments box!';

            else {

                if (strlen($comments) < 15)
                    $errors[] = 'Your explaination is short, please revise!';

                if (strlen($comments) > 45)
                    $errors[] = 'Your explaintion is to long, please revise!';

            }

        }

        if (empty($errors) === false) {
            header('location: signedin.php');
            exit();
        }

        // Validations ends here

        $host     = "localhost"; // Host name
        $username = "root"; // Mysql username
        $password = "testdbpass"; // Mysql password
        $db_name  = "test"; // Database name

        // Connect to server via PHP Data Object
        $dbh = new PDO("mysql:host=localhost;dbname=test;", $username, $password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        try {
            $query = $dbh->prepare("INSERT INTO `students` (anum, FIRST, LAST, why, comments)
                                   VALUES (:anum, :FIRST, :LAST, :why, :comments)");

            $query->execute(

                array(
                    'anum'     => $_POST['anum'],
                    'first'    => $_POST['first'],
                    'last'     => $_POST['last'],
                    'why'      => $_POST['why'],
                    'comments' => $_POST['comments']
                ));
        } catch (PDOException $e) {
            error_log($e->getMessage());
            die($e->getMessage());
        }
        $dbh = null;

    }

}
?>

<html>
<body>
<title>Student Signin Form</title>
<table width="300" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
        <?php
        if (empty($errors) === false) {
            echo '<h3>';
            foreach ($errors as $error) {
                echo '<center><li>', $error, '</li></center>';
            }

            echo '<h3>';
        }
        ?>
    <form action="" method="post">
        <td>
            <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                <tr>

                <tr colspan="3">
                    <center></center>
                    <strong>Student Signin Form</strong></tr>
                <p>Student ID Number: <input type="text" name="anum" <?php if (isset($_POST['anum']) === true) {
                        echo 'value="', $_POST['anum'], '"';
                    } ?> />

                <p>First Name: <input type="text" name="first" <?php if (isset($_POST['first']) === true) {
                        echo 'value="', $_POST['first'], '"';
                    } ?> />

                <p>Last Name: <input type="text" name="last" <?php if (isset($_POST['last']) === true) {
                        echo 'value="', $_POST['last'], '"';
                    } ?> />

                <p>How may we help you? <select name="why"/>
                    <option value=""></option>
                    <option value="Appeal">Appeal</option>
                    <option value="Other">Other: Please specify the nature of your visit bellow</option>
                    </select>
                    </tr>


                    <br>

                <P>If other please describe the issue you are having.</P>
                <textarea rows="10" cols="50" name="comments" <?php if (isset($_POST['comments']) === true) {
                    echo 'value="', $_POST['comments'], '"';
                } ?>></textarea>


                <input type="submit" name="submit" value="Send"/>

    </form>

</table>
</body>
</html>
4

2 に答える 2

1

私が実際に何をしていたか(間違った方法)をさらに掘り下げて理解した後、私は自分の解決策を思いつきました。Mysqlの挿入ステートメントがスタンドアロンではなくエラー検証の一部になるようにする必要がありました。私の以前のコードを見ると、PDOステートメントはコード内に実際の場所がなく、ただそこにありました。この原因は

if (empty($errors) === false) {
        header('location: signedin.php');
        exit();
    }

これが行っていたのは、エラーが発生した場合でも、望ましい効果ではない「signedin.php」にリダイレクトする必要があった場合です。やらなければならなかったのは、最初にそれをfalseからtrueに変更することでした。

if (empty($errors) === true) {
        header('location: signedin.php');
        exit();
    }

その後、{}の間にPDOステートメントを入力する必要があります。

したがって、これが意味するのは、スクリプトがエラーを検出した場合、PDO挿入を実行しないということです。

ただし、エラーがないことがTRUEの場合は、そのスクリプトのエラーチェックを使用して挿入スクリプトを実行し、正しく挿入された場合は、ユーザーを次のページにリダイレクトします。

例 :

if (empty($errors) === true) 
{
            $host="localhost"; // Host name
            $username="root"; // Mysql username
            $password="testdbpass"; // Mysql password
            $db_name="test"; // Database name


            $dbh = new PDO("mysql:host=localhost;dbname=test;", $username, $password);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                    try 
        {
                            $query = $dbh->prepare("INSERT INTO `students` (anum, first, last, why, comments) 
                                   VALUES (:anum, :first, :last, :why, :comments)");

                            $query->execute(
                                                array(
                                                        'anum'      => $_POST['anum'],
                                                        'first'     => $_POST['first'],
                                                        'last'      => $_POST['last'],
                                                        'why'       => $_POST['why'],
                                                        'comments'  => $_POST['comments']
                                                        )); 
        }
                catch (PDOException $e) 
        {
                error_log($e->getMessage());
                die($e->getMessage());
        }
   $dbh = null;    

        header('location: signedin.php');
        exit(); 
}

うまくいけば、誰かがこれをどんな用途でも見つけるでしょう。

于 2012-12-27T23:21:13.880 に答える
0

うまくいくかどうかを実際にテストせずにコードを書いているようです。たとえば、これらの行(約50行目あたり)を見てください。

        if (empty($errors) === false) {
            header('location: signedin.php');
            exit();
        }

$errors配列をエラーメッセージで埋めています。次に、エラーが発生した場合にリダイレクトを実行します。これはエラーメッセージも削除するため、意味がありません。

于 2012-12-27T18:06:39.630 に答える