php在点击提交按钮之前执行

php在点击提交按钮之前执行,php,mysql,syntax-error,Php,Mysql,Syntax Error,我正试图通过PDO连接到我的数据库,就像我的任何其他页面一样,所以我只是从我的其他页面复制并粘贴了相同的代码,没有任何更改(相同的数据库和用户等),现在我遇到了以下错误: [Wed Dec 26 22:51:49 2012][error][client 127.0.0.1]PHP通知: 未定义索引:anum位于/var/www/signinpage.php第69行[Wed Dec] 26 22:51:49 2012][error][client 127.0.0.1]PHP通知:未定义 索引:第7

我正试图通过PDO连接到我的数据库,就像我的任何其他页面一样,所以我只是从我的其他页面复制并粘贴了相同的代码,没有任何更改(相同的数据库和用户等),现在我遇到了以下错误:

[Wed Dec 26 22:51:49 2012][error][client 127.0.0.1]PHP通知: 未定义索引:anum位于/var/www/signinpage.php第69行[Wed Dec]

26 22:51:49 2012][error][client 127.0.0.1]PHP通知:未定义 索引:第70行的/var/www/signipage.php中的第一个[Wed Dec 26]

22:51:49 2012][error][client 127.0.0.1]PHP通知:未定义 索引:最后一个在/var/www/signipage.php第71行[Wed Dec 26 22:51:49] 2012]

[错误][client 127.0.0.1]PHP注意:未定义索引:为什么在 /var/www/signipage.php第72行[Wed Dec 26 22:51:49 2012][错误]

[client 127.0.0.1]PHP注意:未定义索引:中的注释 /var/www/signipage.php第73行[Wed Dec 26 22:51:49 2012][错误]

[client 127.0.0.1]SQLSTATE[23000]:完整性约束冲突: 1048列“anum”不能为null

我认为脚本是在提交按钮之前开始的,我不希望发生这种情况,因为脚本是一个插入和表单验证脚本

这些是连接线(导致错误的地方)

这是完整的代码:

<?php 
//Starting session
session_start();


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


// 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) {
        $errors[] = 'A number is incorrect!';
}
    if(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' && empty($comments)) {
        $errors[] = 'Please explain the nature of your visit in the comments box!';
}
    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) === true) {
        header('location: signedin.php');
            exit(); 
}
} // Validations ends here


// We start our insert statement here!

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>

学生签到表
学生签到表
学生证号码:/>
姓氏:>
扩展您的

if(empty($_POST) === false) {

阻止数据库操作结束。将其关闭
}
放在
$dbh=null之后行。

我刚刚重新启动了mysql,现在我得到另一个错误,“anum”是空的完整性约束。我想脚本是在我没有点击“提交”按钮的情况下运行的;dbname=测试也许你应该删除结尾的分号?我只是通过重新启动mysql修复了这个错误。现在我得到了一个错误,即传递的表单数据是空的,因为我的脚本与表单本身在同一页上。现在我的问题是页面脚本在实际点击submit之前运行。请正确缩进代码。几乎不可能知道你的牙套是如何放置的。
if(empty($_POST) === false) {