简体   繁体   English

使用isset()函数进行PHP表单验证问题

[英]PHP form validation issue with isset() function

i've been learning php form validation and seem to have a confusion in the following thing 我一直在学习php表单验证,并且似乎在以下内容中存在混淆

<html>
<head>
  <title>
    learning
  </title>
</head>

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

<?php
$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"]);
}
?>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
  }

  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = test_input($_POST["website"]);
  }

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = test_input($_POST["comment"]);
  }

  if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
  } else {
    $gender = test_input($_POST["gender"]);
  }
}
?>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "test";
$conn = mysqli_connect($servername,$username,$password,$database);
  if(isset($name) && isset($email) && isset($gender))
  {

      $sql = "insert into learn values('$name','$email','$website','$comment','$gender')";
      if(mysqli_query($conn,$sql))
      {
        echo 'Entry to database successful';
      }
      else {
        echo 'Error!';
      }
  }
 ?>

 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
 Name: <input type="text" name="name">
 <span class="error">* <?php echo $nameErr;?></span>
 <br><br>
 E-mail:
 <input type="text" name="email">
 <span class="error">* <?php echo $emailErr;?></span>
 <br><br>
 Website:
 <input type="text" name="website">
 <span class="error"><?php echo $websiteErr;?></span>
 <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
 <span class="error">* <?php echo $genderErr;?></span>
 <br><br>
 <input type="submit" name="submit" value="Submit">
 </form>

ok so i am trying to make a form validation with php. 好吧所以我试图用PHP进行表单验证。 the page does show the error if one of the required field is empty but still enters the value in the database regardless of the error. 如果其中一个必填字段为空但页面确实显示错误,但仍然输入数据库中的值而不管错误。 I suspect its because of isset because if i replace the line with 我怀疑它是因为isset因为如果我更换线

if(!empty($name) && !empty($email) && !empty($gender))

it seems to work. 它似乎工作。

Edit 1: ok i have managed to fix the issue. 编辑1:好的,我已设法解决问题。 thanks to the people that had helped. 感谢帮助过的人们。

检查此条件而不是现有条件

if(isset($name) && $name != NULL && isset($email) && $email != NULL  && isset($gender) && $gender != NULL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM