我无法使我的php数据库正常工作

时间:2019-02-27 01:14:35

标签: php

因此,我试图将html链接到我的php数据库,但是每当我将浏览器打开到“ localhost / cs / staff / sign_up.php”时,只会出现“名字不应该为空”的消息。 我该如何解决?

----我的'sign_up.html'代码----

    <body>

    <form method="post" action="../sign_up.php" style="border:1px solid #ccc">
      <div class="container">
        <h1>Sign Up</h1>
        <p>Please fill in this form to create an account.</p>
        <hr>
        <label for="Firstname"><b>Firstname</b></label>
        <input type="text" placeholder="Enter Firstname" name="firstname" required>

        <label for="Lastname"><b>Email</b></label>
        <input type="text" placeholder="Enter Lastname" name="lastname" required>

        <label for="email"><b>Email</b></label>
        <input type="text" placeholder="Enter Email" name="email" required>

        <label for="psw"><b>Password</b></label>
        <input type="password" placeholder="Enter Password" name="psw" required>

        <label for="psw-repeat"><b>Repeat Password</b></label>
        <input type="password" placeholder="Repeat Password" name="psw-repeat" required>

        <label>
          <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
        </label>

        <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>

        <div class="clearfix">
          <button type="button" class="cancelbtn">Cancel</button>
          <button type="submit" class="signupbtn">Sign Up</button>
        </div>
      </div>
    </form>

    </body>

---我的'sign_up.php'代码---

    <?php
     $f_name = filter_input(INPUT_POST, 'firstname');
     $l_name = filter_input(INPUT_POST, 'lastname');
     $email = filter_input(INPUT_POST, 'email');
     $password = filter_input(INPUT_POST, 'psw');
      if (!empty($f_name)){
        if (!empty($l_name)){
          if (!empty($email)){
            if (!empty($password)){
              $DB_SERVER = "localhost";
              $DB_USERNAME = "root";
              $DB_PASSWORD = "";
              $DB_NAME = "project";
              // Create connection
              $conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
              if (mysqli_connect_error()){
              die('Connect Error ('. mysqli_connect_errno() .') '
              . mysqli_connect_error());
              }
              else{
              $sql = "INSERT INTO account (firstname, lastname, email, password)
              values ('$firstrname', '$lastname','$email', '$password')";
              if ($conn->query($sql)){
              echo "New record is inserted sucessfully";
              }
              else{
              echo "Error: ". $sql ."
              ". $conn->error;
              }
              $conn->close();
            }
          }
    else{
      echo "Password should not be empty";
          die();
    }
    }
    else{
      echo "Email should not be empty";
        die();
    }
    }
    else{
      echo "Lastname should not be empty";
      die();
     }
    }
     else{
       echo "Firstname should not be empty";
       die();
     }
     ?>

我还是编码新手,如果错误确实很简单且毫无意义,请您谅解。

1 个答案:

答案 0 :(得分:0)

您要将变量firstname的$ _POST值保存在$ f_name上,将姓氏保存在$ l_name上

这是您的代码。

$f_name = filter_input(INPUT_POST, 'firstname');
$l_name = filter_input(INPUT_POST, 'lastname');

您必须更新sql以匹配变量名。

$sql = "INSERT INTO account (firstname, lastname, email, password)
              values ('$f_name', '$l_name','$email', '$password')";