如何抛出验证错误

42fyovps  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(247)

从.php注册

<!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);
      }
      }
 ?>

验证.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;
    }
   ?>

连接.php

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

我正在开发一个简单的注册,有四个输入,即姓名、用户名、密码、电子邮件。当用户填写表单并单击提交按钮时,所有填写的数据都应保存在数据库中,在我的情况下,数据库运行良好,但是,当用户不填写任何数据时,如果用户只需单击提交按钮,则错误消息应显示为“所有字段都是必需的”,但在我的情况下,即使我单击提交按钮而不输入任何值,我得到的消息也是成功的,所有空值都存储在数据库中,这是不应该发生的,我的输出应该是,如果我填写表格,然后单击“提交”按钮,那么所有的数据都应该存储在数据库中,如果我单击“提交”按钮而没有填写任何值,那么错误应该抛出“要填写的所有字段”,并且数据库中不应该存储空值,请任何人都可以指导我应该做什么样的改变,以便得到我想要的输出。

sauutmhj

sauutmhj1#

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = test_input($_POST["name"]);
  $email = test_input($_POST["email"]);
  $website = test_input($_POST["website"]);
  $comment = test_input($_POST["comment"]);
  $gender = test_input($_POST["gender"]);
}

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

<h2>PHP Form Validation Example</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="name">
  <br><br>
  E-mail: <input type="text" name="email">
  <br><br>
  Website: <input type="text" name="website">
  <br><br>
  Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  <br><br>
  Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <input type="radio" name="gender" value="other">Other
  <br><br>
  <input type="submit" name="submit" value="Submit">  
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
r3i60tvu

r3i60tvu2#

如果您不介意再添加一点代码,您的代码如下:
在您的注册表中\u form.php

<?php
require('validation.php'); // Require first to do validation before queries
require('connect.php');

// Remove the part where you set variables to $_POST params
// Variables are already set inside validation.php

/**

* Then, I recommend moving queries to**connect.php**
* to have all your sql functions inside one file.
* Also moving the inserting of data to a function for easy grouping/calling
* /

if (isset($_POST["submit"]) {
    // Check if validation does not fail
    if ($emailErr == "" || $nameErr == "" || $userErr == "" || $passwordErr == "") {
        // Call to insert function
        doInsert($name, $email, $username, $password);
    } else {
        echo $emailErr . " " . $nameErr . " " . $userErr . " " . $passwordErr;
    }
}

?>

在connect.php中

function doInsert($name, $email, $username, $password) {
    $connect = mysqli_connect("localhost", "root", "","php_forum")
        or die("Error " . mysqli_error($connect));

    $sql = "INSERT INTO users(`id`,`name`,`username`, `password`, `email`)
            VALUES ('','".$name."', '".$username."', '".$password."', '".$email."')";

    $query = mysqli_query($connect, $sql);

    if ($query) {
        echo "Success";
    } else {
        echo "Failure " . mysqli_error($connect);
    }
}
vq8itlhq

vq8itlhq3#

请在会话中添加错误并在表单文件中打印会话。
在validation.php中

$nameErr = $emailErr = $userErr = $passwordErr = "";
$name =  $email = $username =$password = "";
if (isset($_POST['submit'])) {

 $name = $_POST["name"];
 $email = $_POST["email"];
 $username = $_POST["username"];
 $password = $_POST["password"];

if($name == '' ||  $email == '' || $username == '' || $password == "") 
 {
  echo "ALL FIELDS ARE NECESSARY";
  exit();
 }

     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"]);
      }

  }

从.php注册

if(isset($_SESSION['error]) && !empty($_SESSION['error])){
   echo $_SESSION["error"]
 }

相关问题