PHP表单空验证不起作用

时间:2016-04-05 16:27:59

标签: php mysql

我创建了我的表单,除了q1-3的最后3个if语句之外,我的大部分验证似乎都有效。我尝试了许多不同的方法,但如果我将表单留空,则无法提出最后3条错误消息。

如果有人能注意到我做错了什么或有任何想法?

SELECT 
CLIENTS.ID AS CLIENT, CLIENTS.NAME AS NAME, AGENTS.NAME AS AGENT FROM CLIENTS  
LEFT JOIN AGENTS ON  AGENTS.CLIENTID = CLIENTS.ID  
WHERE CLIENTS.NAME LIKE '%"&CLIENTNAME&"%' AND AGENTS.NAME LIKE '%"&AGENTNAME&"%'" 

HTML:

if (isset($_POST['submit']))
    {
  $errorMessage = "";

  if(empty($_POST['UserName'])) 
  {
    $errorMessage .= "<li>You forgot to enter a UserName!</li>";
  }
  if(empty($_POST['Password'])) 
  {
    $errorMessage .= "<li>You forgot to enter a Password!</li>";
  }
   if(empty($_POST['Password2'])) 
  {
    $errorMessage .= "<li>You forgot to confirm your Password!</li>";
  }
   if(empty($_POST['FirstName'])) 
  {
    $errorMessage .= "<li>You forgot to enter a First Name!</li>";
  }
   if(empty($_POST['LastName'])) 
  {
    $errorMessage .= "<li>You forgot to enter a Last Name!</li>";
  }
   if(empty($_POST['Gender'])) 
  {
    $errorMessage .= "<li>You forgot to select a Gender!</li>";
  }
   if(empty($_POST['DOB'])) 
  {
    $errorMessage .= "<li>You forgot to enter your DOB!</li>";
  }
  if(empty($_POST['Email'])) 
  {
    $errorMessage .= "<li>You forgot to enter your Email!</li>";
  }
   if(empty($_POST['q1'])) 
  {
    $errorMessage .= "<li>You forgot to answer question 1!</li>";
  }
  if(empty($_POST['q2'])) 
  {
    $errorMessage .= "<li>You forgot to answer question 2!</li>";
  }
  if(empty($_POST['q3'])) 
  {
    $errorMessage .= "<li>You forgot to answer question 3!</li>";
  }
  //if they are not then set them to this
   $UserName = $_POST['UserName']; 
    $Password = $_POST['Password'];
    $Password2 = $_POST['Password2'];
    $FirstName = $_POST['FirstName'];
    $LastName = $_POST['LastName'];
    $Gender = $_POST['Gender'];
    $DOB = $_POST['DOB'];
    $Email = $_POST['Email'];
    $Q1 = $_POST['q1'];
    $Q2 = $_POST['q2'];
    $Q3 = $_POST['q3'];



  if(!empty($errorMessage)) 
  {
    echo("There was an error with your form:\n");
    echo("<ul>" . $errorMessage . "</ul>\n");
  } 
 else{ 
 $result=mysqli_query($db, "INSERT INTO `users` (UserName, Password, Password2, FirstName, LastName, Gender, DOB, Email, q1, q2, q3) 
    VALUES ('$UserName', '$Password', '$Password2', '$FirstName', '$LastName', '$Gender', '$DOB', '$Email', '$Q1', '$Q2', '$Q3') ")or die(mysqli_error($db));
     header('location:welcome-page.php');
    exit;
}

1 个答案:

答案 0 :(得分:0)

试试这个让我知道!

$UserName = $_POST['UserName']; 
$Password = $_POST['Password'];
$Password2 = $_POST['Password2'];
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$Gender = $_POST['Gender'];
$DOB = $_POST['DOB'];
$Email = $_POST['Email'];
$Q1 = $_POST['q1'];
$Q2 = $_POST['q2'];
$Q3 = $_POST['q3'];

if (isset($_POST['submit']))
{
    $errorMessage = "";
    if(empty($_POST['UserName'])) {
        $errorMessage .= "<li>You forgot to enter a UserName!</li>";
    }
    if(empty($_POST['Password'])) {
        $errorMessage .= "<li>You forgot to enter a Password!</li>";
    }
    if(empty($_POST['Password2'])) {
        $errorMessage .= "<li>You forgot to confirm your Password!</li>";
    }
    if(empty($_POST['FirstName'])) {
        $errorMessage .= "<li>You forgot to enter a First Name!</li>";
    }
    if(empty($_POST['LastName'])) {
        $errorMessage .= "<li>You forgot to enter a Last Name!</li>";
    }
    if(empty($_POST['Gender'])) {
        $errorMessage .= "<li>You forgot to select a Gender!</li>";
    }
    if(empty($_POST['DOB'])) {
        $errorMessage .= "<li>You forgot to enter your DOB!</li>";
    }
    if(empty($_POST['Email'])) {
        $errorMessage .= "<li>You forgot to enter your Email!</li>";
    }
    if(empty($_POST['q1'])) {
        $errorMessage .= "<li>You forgot to answer question 1!</li>";
    }
    if(empty($_POST['q2'])) {
        $errorMessage .= "<li>You forgot to answer question 2!</li>";
    }
    if(empty($_POST['q3'])) {
        $errorMessage .= "<li>You forgot to answer question 3!</li>";
    }
}

if(!empty($errorMessage)){
    echo("There was an error with your form:\n");
    echo("<ul>" . $errorMessage . "</ul>\n");
}else{
    $result=mysqli_query($db, "INSERT INTO `users` (UserName, Password, Password2, FirstName, LastName, Gender, DOB, Email, q1, q2, q3) VALUES ('$UserName', '$Password', '$Password2', '$FirstName', '$LastName', '$Gender', '$DOB', '$Email', '$Q1', '$Q2', '$Q3') ") or die(mysqli_error($db));
    header('location:welcome-page.php');
    exit;
}