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(!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";
  }

     }

 ?>

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

4

3 回答 3

1

以下测试是错误的:

if(!empty($error))

应该 :

if(empty($error))

而且你的 SQL 也是错误的......应该是:

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

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

于 2013-04-08T11:47:54.563 回答
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
  }
}
于 2013-04-08T11:47:43.797 回答
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";
  }
   }
 ?>
于 2013-04-08T11:49:23.363 回答