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

时间:2012-12-27 03:11:56

标签: php mysql syntax-error

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

  

[Wed Dec 26 22:51:49 2012] [错误] [客户端127.0.0.1] PHP通知:   未定义的索引:第69行的/var/www/signinpage.php中的anum [Wed Dec

     

26 22:51:49 2012] [错误] [client 127.0.0.1] PHP注意:未定义   index:首先在第70行的/var/www/signinpage.php [12月26日星期三

     

2012年22:51:49] [错误] [客户端127.0.0.1] PHP注意:未定义   index:最后在第71行的/var/www/signinpage.php [Wed Dec 26 22:51:49   2012]

     

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

     

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

     

[client 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>

1 个答案:

答案 0 :(得分:1)

扩展你的

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

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