PHP 代码未提交到 mysql 数据库

标签 php mysql

我正在尝试为网站创建登录信息。到目前为止,每当我发布到数据库时都不会显示提交的信息,唯一发布的内容是哈希密码。

<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 block

<?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 injections持开放态度。

关于PHP 代码未提交到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32298615/

相关文章:

c# - 调试 C# 应用程序

php - 严格标准的 PHP 错误蛋糕

php - 是否有 Google Plus 评论插件? (比如 Facebook 社交评论、DISQUS 或 IntenseDebate)?

php - 有没有办法 var_dump 当前事件的无缓冲查询

php - MySQL 导出导入,外键 ID 自动递增

php - 测试查询变量是否存在并且还设置为特定值?

php - yii2 - 如何解决 Bad Request (#400) 无法验证您的数据提交?

php - MYSQL SELECT 语句 Where last Date If Trace = 12

mysql - 将 MySQL 数据库导入 SQL Server

MySQL:需要检索所有记录但忽略旧条目