错误消息未在PHP中显示以进行验证

时间:2013-04-08 11:44:03

标签: php phpmyadmin

以下是PHP代码 数据库文件工作正常。     

   if(isset($_POST['submit']))
   {
    $error = array();

 if(empty($_POST["fname"]))
   {
     $error[] = "Please Enter a name";
   }
  else
   {
      $fname = $_POST["fname"];
   }

    if(empty($_POST["lname"]))
     {
  $error[] = "Please Enter last name";
}
   else
     {
  $lname = $_POST["lname"];
}

    if(empty($_POST["email"]))
     {
   $error = "Enter email Id";
 }
  else
     {
   if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-        9\._-]+)+$/", $_POST["email"]))
   {
     $email = $_POST["email"];
   }
  else
   {
     $error = "Enter a vaild Email Id";
   }       
    }

   if(empty($_POST["password"]))
    {
  $error = "Enter a password";
}
   else
    {
  $password = $_POST["password"];
}

    if(!empty($error))
      {
  $sql = "SELECT * FROM form (id, 'FirstName', 'LastName', 'Email', 'Password')  VALUES('', '$fname', '$lname', '$email', '$password')";
   $result = mysql_query($sql);
   echo "Successfully Register";
  }
    else
      {
   foreach($error as $key => $values)
                  {
                    echo ' <li>' . $values . '</li>';
                  }
                echo '</ol>';
    echo "Error";
  }

     }

 ?>

上面的代码没有显示任何错误消息...如果我提交表单只有空白页面ll出现...我使用上面的代码验证我的表单,但它只是我使用的基本方法,并为每个我使用显示错误......

3 个答案:

答案 0 :(得分:1)

以下测试错误:

if(!empty($error))

应该是:

if(empty($error))

你的SQL也错了......应该是:

$sql = "Insert into form (FirstName, LastName, Email, Password)  VALUES('$fname', '$lname', '$email', '$password')";

假设您的id字段是自动递增的

答案 1 :(得分:0)

您忘记将错误推送到数组。

$error = "Enter a password"; //$error is no more an array. It is a string

必须在几个地方:

$error[] = "Enter a password";

另外,我建议您使用嵌套的if语句:

if (!empty($_POST['submit'])){
  $errors = array() ;
  if (!isset($_POST['email'])
    $errors['email'] = "No email" ;
  //And so on.

  //Then check for errors
  if (!empty($errors)){
    //proceed submission
  }
}

答案 2 :(得分:0)

试试这段代码,它适用于您。

<?php 
  if(isset($_POST['submit']))
   {
    $error = array();

 if(empty($_POST["fname"]))
   {
     $error[] = "Please Enter a name";
   }
  else
   {
      $fname = $_POST["fname"];
   }

    if(empty($_POST["lname"]))
     {
  $error[] = "Please Enter last name";
}
   else
     {
  $lname = $_POST["lname"];
}

    if(empty($_POST["email"]))
     {
   $error[] = "Enter email Id";
 }
  else
     {
   if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-        9\._-]+)+$/", $_POST["email"]))
   {
     $email = $_POST["email"];
   }
  else
   {
     $error[] = "Enter a vaild Email Id";
   }       
    }

   if(empty($_POST["password"]))
    {
  $error[] = "Enter a password";
}
   else
    {
  $password = $_POST["password"];
}
    if(count($error)<=0)
      {
  $sql = "SELECT * FROM form (id, 'FirstName', 'LastName', 'Email', 'Password')  VALUES('', '$fname', '$lname', '$email', '$password')";
   $result = mysql_query($sql);
   echo "Successfully Register";
  }
    else
      {
   foreach($error as $key => $values)
                  {
                    echo ' <li>' . $values . '</li>';
                  }
                echo '</ol>';
    echo "Error";
  }
   }
 ?>