isset功能无法正常工作

时间:2017-11-10 15:02:55

标签: php

我正在尝试将记录插入表中,但我在isset函数上遇到问题。我无法处理代码。

在以下代码中执行else语句而不是isset函数。

 <?php
if (isset($_POST['submit'])) {

    include_once 'dbh.inc.php';

    $firstname = mysqli_real_escape_string($conn, $_POST['firstname']);
    $surname = mysqli_real_escape_string($conn, $_POST['surname']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);
    $cpassword = mysqli_real_escape_string($conn, $_POST['cpassword']);
    $bdate = mysqli_real_escape_string($conn, $_POST['bdate']);
    $gender = mysqli_real_escape_string($conn, $_POST['gender']);

    if(empty($firstname) || empty($surname) || empty($email) || empty($username) || empty($password) || empty($cpassword) || empty($bdate) || empty($gender)) {
       header("Location: ../index.php?index=empty");
       exit();
    } else {
        $sql = "SELECT * FROM users WHERE username='$username'";
        $result = mysqli_query($conn,$sql);
        $resultCheck = mysqli_num_rows($result);
        if ($resultCheck > 0) {
            header("Location: ../index.php?index=username already taken");
            exit();
        } else {
            // Hashing the password
            $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
            //Insert users into the database
            $sql = "INSERT INTO users (id, firstname, surname, email, username, password, bdate, gender) VALUES ('', '$firstname', '$surname', '$email', '$username', '$password', '$bdate', '$gender');";
            mysqli_query($conn, $sql);
            header("Location: ../profile.php");
            exit();
        }
    }
} else {
    header("Location: ../index.php?index=error");
    exit();
}

这是包含“注册”部分的前端:

<div class="container and">
  <div class="row">
    <div class="col-lg-6 pull-right">
 <div class="panel panel-default">
  <div class="panel-heading">
    <h3>Create a new account.</h3>
  </div>
   <div class="panel-body">
     <form id="loginform" action="includes/signup.inc.php" method="POST">
       <div class="row">
         <div class="form-group has-error col-md-6">
           <input type="text" class="form-control" name="firstname" placeholder="First Name">
         </div>
         <div class="form-group has-error col-md-6">
           <input type="text" class="form-control" name="surname" placeholder="Surname">
         </div>
       </div>
       <div class="row padding-top-10">
                    <div class="form-group has-error col-md-12">
                      <input type="text" class="form-control" name="email" placeholder="Email Address">
                    </div>
        </div>
       <div class="row padding-top-10">
         <div class="form-group has-error col-md-12">
           <input type="text" class="form-control" name="username" placeholder="Username">
         </div>
       </div>
         <div class="row padding-top-10">
           <div class="form-group has-error col-md-12 padding-top-10">
             <input type="password" class="form-control" name="password" placeholder="Password">
           </div>
         </div>
           <div class="row padding-top-10">
             <div class="form-group has-error col-md-12">
               <input type="password" class="form-control" name="cpassword" placeholder="Confirm Password">
             </div>
           </div>
         <div class="row padding-top-10">
           <div class="form-group has-error col-md-12">
             <h4>&nbsp;&nbsp; Birthday</h4>
             <input type="date" class="form-control" name="bdate" value="birthdate">
           </div>
         </div>
         <div class="row padding-top-10">
           <div class="form-group has-error col-md-6 padding-top-10">
             <div class="pull-right">
            Male: <input type="radio" name="gender" value="male" />
           </div>
           </div>
           <div class=" form-group has-error col-md-6 padding-top-10">
             Female: <input type="radio" name="gender" value="female">
           </div>
       </div>
       <div class="padding-top-10">
         <button class="btn btn-success" name="submit">Create Account</button>
       </div>
     </form>
   </div>
 </div>
</div>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

您错过了按钮类型属性type="submit"如果您未包含此类型属性,则表单将不会提交数据。

您的代码:

<button class="btn btn-success" name="submit">Create Account</button>

应该是:

<button type="submit" class="btn btn-success" name="submit">Create Account</button>

如果,这不是你的问题,请告诉我!感谢。