-1

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

[Wed Dec 26 22:51:49 2012] [error] [client 127.0.0.1] PHP Notice: Undefined index: anum in /var/www/signinpage.php on line 69 [Wed Dec

26 22:51:49 2012] [error] [client 127.0.0.1] PHP Notice: Undefined index: first in /var/www/signinpage.php on line 70 [Wed Dec 26

2012 年 22:51:49] [错误] [客户端 127.0.0.1] PHP 通知:未定义索引:最后在 /var/www/signinpage.php 第 71 行 [2012 年 12 月 26 日星期三 22:51:49]

[错误] [客户端 127.0.0.1] PHP 通知:未定义索引:为什么在第 72 行的 /var/www/signinpage.php [2012 年 12 月 26 日星期三 22:51:49] [错误]

[client 127.0.0.1] PHP 注意:未定义索引:第 73 行 /var/www/signinpage.php 中的注释 [2012 年 12 月 26 日星期三 22:51:49] [错误]

[客户端 127.0.0.1] SQLSTATE [23000]:违反完整性约束:1048 列 'anum' 不能为空

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

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

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

这是完整的代码:

<?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>
4

1 回答 1

1

扩展您的

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

阻塞到数据库操作结束。}把它放在行后关闭$dbh = null;

于 2012-12-27T04:20:48.333 回答