簡體   English   中英

PHP代碼未提交到mysql數據庫中

[英]PHP code isn't submitting into mysql database

我正在嘗試為網站創建登錄名。 到目前為止,只要我發布到數據庫中不顯示已提交的信息,發布的唯一內容就是哈希密碼。

<form method="post" action="submit.php">
                        <div class="form-group">
                            <label for="email">Email:</label>
                            <input type="email" class="form-control" id="email">
                        </div>
                        <div class="form-group">
                            <label for="username">Username:</label>
                            <input type="text" class="form-control" id="username">
                        </div>
                        <div class="form-group">
                            <label for="password">Password:</label>
                            <input type="password" class="form-control" id="password">
                        </div>
                        <div class="form-group">
                            <label for="pwd2">Re-Password</label>
                            <input type="password" class="form-control" id="pwd2">
                        </div>
                        <div class="form-group">
                            <input type="submit" class="form-control" id="submit">
                        </div>
                    </form>

提交到這個PHP塊

<?php
    $servername = "localhost";
    $username = "root";
   $password = "Password";
   $dbname = "DBNAME";


   $email = NULL;
   $user = NULL;
   $pass1 = NULL;

   if (isset($_POST['email'])){
   $email = $_POST['email'];
    }
   if (isset($_POST['username'])){
   $user = $_POST['username'];
   }

    if (isset($_POST['password'])){
   $pass1 = $_POST['password'];
   }
  $hash = password_hash($pass1, PASSWORD_BCRYPT);


   $conn = new mysqli($servername, $username, $password, $dbname);

   if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
   } 

   $sql = "INSERT INTO Users (email, username, password )
   VALUES ('$email', '$user', '$hash')";

   if ($conn->query($sql) === TRUE) {
   echo "New record created successfully";
   } else {
   echo "Error: " . $sql . "<br>" . $conn->error;
    }

  $conn->close();
  ?>

您的表單字段缺少名稱屬性。 沒有它們,就不會向您的腳本發送任何值。 這可以通過執行var_export($_POST)輕松測試。

<form method="post" action="submit.php">
    <div class="form-group">
        <label for="email">Email:</label>
        <input type="email" class="form-control" name="email" id="email">
    </div>
    <div class="form-group">
        <label for="username">Username:</label>
        <input type="text" class="form-control" name="username" id="username">
    </div>
    <div class="form-group">
        <label for="password">Password:</label>
        <input type="password" class="form-control" name="password" id="password">
    </div>
    <div class="form-group">
        <label for="pwd2">Re-Password</label>
        <input type="password" class="form-control" name="pwd2" id="pwd2">
    </div>
    <div class="form-group">
        <input type="submit" class="form-control" id="submit">
    </div>
</form>

僅供參考,您對SQL注入持開放態度

暫無
暫無

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

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