2012-12-27 54 views
-1

我想通过PDO连接到我的数据库,就像我的其他页面一样,所以我只是从我的其他页面复制并粘贴相同的代码没有任何改变(相同的数据库和用户等)和现在我收到此错误:PHP在提交按钮被击中之前执行

[星期三12月26日22时51分49秒2012] [错误] [客户端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注意:未定义 index:首先在/var/www/signinpage.php在第70行[Wed Dec 26

22:51:49 2012] [错误] [客户端127.0.0.1] PHP注意:未定义 index:上一个/var/www/signinpage.php在线71 [Wed Dec 26 22:51:49 2012 ]

[错误] [客户端127.0.0.1] PHP说明:未定义指数:为什么在 /var/www/signinpage.php上线72 [星期三12月26日22点51分49秒2012] [错误]

[127.0.0.1客户] PHP说明:未定义指数:在 /var/www/signinpage.php上线73 [星期三12月26日22时51分49秒2012] [错误]

[客户端127.0注释。 0.1] SQ LSTATE [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> 
+0

我刚刚重新启动mysql,现在我得到另一个错误,“anum”是空的完整性约束。我认为脚本正在运行,没有我点击“提交”按钮。 –

+0

查看你有'mysql:host = localhost; dbname = test;'的行;'也许你应该删除结尾的分号? – Stegrex

+0

我jut通过重新启动mysql现在解决了错误我得到错误,有空表单数据被传递,因为我有脚本在窗体本身在同一页上。现在我的问题是,页面脚本正在运行之前实际上提交。 –

回答

1

扩展您

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

块到结束的数据库操作。把它关闭}$dbh = null;线。

+0

谢谢你的帮助先生! –