不显示错误消息

时间:2013-10-23 08:03:13

标签: php mysql

我是这个php的新手。我正在尝试对我的表单进行一些验证,如果它利用我的验证规则将显示错误消息。  我的连接文件。

<?php
$con = mysql_connect("localhost","root","") or die('could not connect the server: '. mysql_error());
mysql_select_db("interview",$con);
?>

我的validate.php文件

<?php
require_once('connect.php');
$realnameErr = $nickErr = $passwordErr = $emailErr = "";
$realname = $nick = $password = $email = "";
?>

我的表格

<form name='v2' id='login' method='post' action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

                     <fieldset>
<legend>Login</legend>
<label for='realname' >Real Name*:</label>
<input type='text' name='realname' id='realname'  maxlength="50" value="<?php echo $realname;?>" /></br>
<span class="error"><?php echo $realnameErr;?></span>
 <br>

<label for='nick' >Nick*:</label>
<input type='text' name='nick' id='nick'  maxlength="50" value="<?php echo $nick;?>" /></br>
 <span class="error"><?php echo $nickErr;?></span>
 <br>
 <label for='password' >Password*:</label>
 <input type='password' name='password' id='password' maxlength="50" /></br>
 <span class="error"><?php echo $passwordErr;?></span>
<br>
  <label for='email' >Email*:</label>
<input type='text' name='email' id='email'  maxlength="50" value="<?php echo $email;?>"/></br>
</fieldset>
<input type='submit' name='submit' value='submit' />
 </form>

验证从这里开始

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['submit']))  {
if (empty($_POST["realname"]))
{
$realnameErr = "Name is required"; 
}
else
{
$realname=test_input($_POST["realname"]);
if(!preg_match("/^[a-zA-z ]*$/",$realname))
{
$realnameErr = "only letters and white space allowed";
}}
if(empty($_POST["nick"]))
{
$nickErr = "Nick is required";
}
  else {
$nick=($_POST["nick"]);
}
     if(empty($_POST["password"])) 
{
$passwordErr = "password is required";
}
else {
$password=($_POST["password"]);
}
 if(empty($_POST["email"]))
{
$emailErr = "email is required";
}
else {
$email=test_input($_POST["email"]);
if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}}

检查然后插入

if((!$realnameErr) && (!$nickErr) && (!$passwordErr) && (!$emailErr))  {
$query="INSERT INTO `main`"."(realname,nick,password,email)". "VALUES". "('$realname','$nick',SHA('$password'),'$email')";
     $res=mysql_query($query);
echo '<p>Your account has been Successfully created,You are now ready to login. </p>';
     }
 }}

function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

2 个答案:

答案 0 :(得分:3)

在显示表单之前,您需要拥有自己的工作脚本。因为此时输出<span class="error"><?php echo $nickErr;?></span>变量$nickErr的时间仍为空,因此不会显示任何内容。

答案 1 :(得分:0)

试试这个:

// Init
$errors = array();

// Validate Post Data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['submit'])) {
        if (empty($_POST["realname"])) {
            $errors[] = "Name is required";
        } else {
            $realname = test_input($_POST["realname"]);
            if (!preg_match("/^[a-zA-z ]*$/", $realname)) {
                $errors[] = "only letters and white space allowed";
            }
        }
        if (empty($_POST["nick"])) {
            $errors[] = "Nick is required";
        } else {
            $nick = ($_POST["nick"]);
        }
        if (empty($_POST["password"])) {
            $errors[] = "password is required";
        } else {
            $password = ($_POST["password"]);
        }
        if (empty($_POST["email"])) {
            $errors[] = "email is required";
        } else {
            $email = test_input($_POST["email"]);
            if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
                $errors[] = "Invalid email format";
            }
        }
    }
}

// If there is any error
if (sizeof($errors))
{
    // display it
    echo '<div>Following error(s) occured:<br /><br />'. implode('<br />', $errors) .'</div>';
}
else
{
    // proceed with db insert here
}