簡體   English   中英

我無法讓我的 php 數據庫正常工作

[英]I can't get my php data base to work correctly

所以我試圖將我的 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();
     }
     ?>

如果錯誤真的很簡單和noobish,我仍然是編碼新手,所以很抱歉。

您將 $f_name 上的變量 firstname 和 lastname 的 $_POST 值保存在 $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')";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM