无法抛出错误消息

时间:2018-04-20 06:36:58

标签: php html mysql

registration_form

 <!DOCTYPE HTML>
    <html>
   <head>
    <title>Register</title>
   </head>
   <body>
    <form action="" method="POST">
    Name: 
    <input type="text" name="name">
    <br/> <br/>
    Username: 
    <input type="text" name="username">
    <br/> <br/>
    Password:
    <input type="password" name="password">
    <br/> <br/>

    Email: 
    <input type="text" name="email">
    <br/> <br/>
    <input type="submit" name="submit" value="Register">
</form>
</body>
    </html>
  <?php
 require('connect.php');
  require('validation.php');
    $name = $_POST['name'];
  $username = $_POST['username'];
       $password = $_POST['password'];

  $email = $_POST['email'];

     if(isset($_POST["submit"])){
       if($query = mysqli_query($connect,"INSERT INTO users 
     (`id`,`name`,`username`, `password`, `email`) VALUES ('','".$name."', 
      '".$username."', '".$password."', '".$email."')")){
          echo "Success";
      }else{
         echo "Failure" . mysqli_error($connect);
         }
           }
   ?>

validation.php

     <?php
      // define variables and set to empty values
      $nameErr = $emailErr = $userErr = $passwordErr = "";
      $name =  $email = $username =$password = "";


       if (isset($_POST['submit'])) {
        if (empty($_POST["name"])) {
         $nameErr = "Name is required";
       } else {
        $name = test_input($_POST["name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $nameErr = "Only letters and white space allowed"; 
       }
        }

        if (empty($_POST["email"])) {
         $emailErr = "Email is required";
         } else {
       $email = test_input($_POST["email"]);
         // check if e-mail address is well-formed
         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
           $emailErr = "Invalid email format"; 
         }
         }

        if (empty($_POST["username"])) {
       $userErr = "Username is required";
       } else {
        $username = test_input($_POST["username"]);
          }

        if (empty($_POST["password"])) {
         $passwordErr = "Password is required";
       } else {
       $password= test_input($_POST["password"]);
          }

     }

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

connect.php

    <?php
     $connect = mysqli_connect("localhost", "root", "","php_forum") 
    or die("Error " . mysqli_error($connect)); 
       ?>

我正在开发一个简单的注册,有四个输入,即名称,用户名,密码,电子邮件。当用户填写表单并单击提交按钮时,所有填充的数据应该保存在正在工作的数据库中在我的情况下很好,但是当用户不会填写任何数据并且用户只是单击提交按钮时,错误消息应该显示为“所有字段都是必需的”,但在我的情况下,即使我单击提交按钮而不输入任何值我得到了成功的消息,并且所有的空值都存储在数据库中,这不应该发生,我的输出应该是如果我填写表单n点击提交按钮然后所有数据应该存储在数据库中,如果我单击提交按钮而不填写任何值,然后错误应该抛出“所有要填充的字段”并且不应将空值存储在数据库中,请任何人指导我应该做什么更改以获得我想要的输出。

1 个答案:

答案 0 :(得分:0)

你做

require('validation.php');

设置和检查很多变量,例如

 $name = test_input($_POST["name"]);
 $nameErr = "Only letters and white space allowed"; 

从require返回,并使用原始发布值覆盖$ name,并且不对$ nameErr

执行任何操作
 $name = $_POST['name'];

检查require

后的错误

e.g。

require('validation.php');
$lsError = $nameErr . $emailErr . $userErr . $passwordErr;
if(trim($lsError) != '') {
    echo $lsError ."<BR>";
    echo "ALL FIELDS ARE NECESSARY";
}
else {
   //rest of your insert
}