-3

我已经尝试调试这个很久了,不明白为什么它不向我的数据库发送数据。另外作为一个指示,我正在使用 Boostrap 并且此表单位于模态框内。有人可以帮帮我吗?我将包括我的 html 片段以及 php 代码。

代码

<form class="form-horizontal" role="form" action="register.php" method="POST">
      <div class="input-group margin-bottom-sm"><span class="input-group-addon"><i class="glyphicon glyphicon-user" aria-hidden="true"></i></span>                        <input class="form-control" type="text" name="fname" id="FirstName" placeholder="First Name" required>
           </div></br>
          
        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user" aria-hidden="true"></i></span>
                    <input class="form-control" type="text" name="lname" id="LastName" placeholder="Last Name" required>
        </div></br>
        
        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-calendar" aria-hidden="true"></i></span>
                    <input class="form-control" type="text" name="dob" id="dob" placeholder="Date of Birth" required>
        </div><br>
        
        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-book" aria-hidden="true"></i></span>
                    <input class="form-control" type="text"  name="school" id="SchoolName" placeholder="School" required>
        </div></br>
    
        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user" aria-hidden="true"></i></span>
                    <input class="form-control" type="text" name="username" id="Username" placeholder="Username" required>
        </div></br>
      
        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-lock" aria-hidden="true"></i></span>
                    <input class="form-control" type="password"  name="pass" data-minlenght= "6" id="Pass" placeholder="Password" required> 
        </div><div class="help-block">Minimum of 6 characters</div></br></br>
        
        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-lock" aria-hidden="true"></i></span>
                    <input class="form-control" type="password" id="inputPasswordConfirm" data-match="#inputPassword"
                           data-match-error="Whoops, these don't match" placeholder="Confirm" required>
        </div></br>
    
        <div class="form-group">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary" id="register">Sing Up</button>
        </div>
    
        </form>

PHP

<?php

$conn=mysqli_connect("localhost","mydatabase","mypassword")
    or die("Could not connect:".mysqli_error($conn));
mysqli_select_db($conn , 'mydatabase') or die ('Database will not open');

if(isset($_POST['register'])){


    $fname = ($_POST['FirstName']);  
    $lname = ($_POST['LastName']);
    $dob = ($_POST['dob']);
    $school = ($_POST['SchoolName']);
    $username = ($_POST['Username']);
    $pass = ($_POST['Pass']);


    $query = "INSERT INTO users (FirstName,LastName,dob,SchoolName,Username,Pass)VALUES('$FirstName','$LastName','$Username','$dob', '$SchoolName','$Pass',)";
    $result = mysqli_query($conn,$query);
}
?>
4

4 回答 4

3

您的所有 $_POST 变量都未定义。您应该使用表单中的 name 属性来分配 $_POST 值,而不是ID来自输入

<form class="form-horizontal" role="form" action="register.php" method="POST">
      <div class="input-group margin-bottom-sm"><span class="input-group-addon"><i class="glyphicon glyphicon-user" aria-hidden="true"></i></span>                        <input class="form-control" type="text" name="fname" id="FirstName" placeholder="First Name" required>
           </div></br>

        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user" aria-hidden="true"></i></span>
                    <input class="form-control" type="text" name="lname" id="LastName" placeholder="Last Name" required>
        </div></br>

        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-calendar" aria-hidden="true"></i></span>
                    <input class="form-control" type="text" name="dob" id="dob" placeholder="Date of Birth" required>
        </div><br>

        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-book" aria-hidden="true"></i></span>
                    <input class="form-control" type="text"  name="school" id="SchoolName" placeholder="School" required>
        </div></br>

        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user" aria-hidden="true"></i></span>
                    <input class="form-control" type="text" name="username" id="Username" placeholder="Username" required>
        </div></br>

        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-lock" aria-hidden="true"></i></span>
                    <input class="form-control" type="password"  name="pass" data-minlenght= "6" id="Pass" placeholder="Password" required> 
        </div><div class="help-block">Minimum of 6 characters</div></br></br>

        <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-lock" aria-hidden="true"></i></span>
                    <input class="form-control" type="password" id="inputPasswordConfirm" data-match="#inputPassword"
                           data-match-error="Whoops, these don't match" placeholder="Confirm" required>
        </div></br>

        <div class="form-group">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary" id="register" name="register">Sing Up</button>
        </div>

        </form>

然后你应该使用准备好的语句将数据保存在你的数据库中,即使你在本地主机上工作,也不要在数据库中存储纯文本密码事实上永远不要存储密码,只需使用password_hash()和存储密码的哈希值password_verify()

if(isset($_POST['register'])){


    //validate these
    $fname = ($_POST['fname']);  
    $lname = ($_POST['lname']);
    $dob = ($_POST['dob']);
    $school = ($_POST['school']);
    $username = ($_POST['username']);
    $pass = ($_POST['pass']);

    $hash = Password_hash($pass,PASSWORD_DEFAULT); //hash password

    $query = $conn->prepare("INSERT INTO users (FirstName,LastName,dob,SchoolName,Username,Pass) VALUES(?,?,?,?,?,?)");
    $query->bind_param("ssssss",$fname,$lname,$dob,$school,$username,$hash);

    if($query->execute()){

        echo "data inserted success";
    }
}
?>

注意:如果我们要插入来自外部源的任何数据(如用户输入),对数据进行清理和验证非常重要。

然后password_verify();将在您的登录页面上运行,它基本上将用户输入的密码与存储在数据库中的哈希值进行比较。

有关的更多信息password_hash()password_verify();查看手册:

http://php.net/manual/en/function.password-hash.php

http://php.net/manual/en/function.password-verify.php

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php准备好的语句。

于 2017-03-09T14:56:59.373 回答
2

所以让我们稍微澄清一下..

首先,您的查询顺序完全不正确(请注意:)FirstName,LastName,dob这意味着前 3 个值应该是$fname,$lname,$dob,但是您$username出于某种原因添加了..

注意:您正在添加不存在的变量,例如$FirstName实际分配的变量实际上是:$fname.

您的查询(记下最后一个变量$Pass,),因为这是您不添加逗号的最后一个变量条目,请看一下:

$query = "INSERT INTO users (FirstName,LastName,dob,SchoolName,Username,Pass)VALUES('$FirstName','$LastName','$Username','$dob', '$SchoolName','$Pass',)";

应该:

$query = "INSERT INTO users (`FirstName`,`LastName`,`dob`,`SchoolName`,`Username`,`Pass`)VALUES('$fname','$lname','$dob','$school','$username','$pass')";

更好的是,您可以绑定参数以防止 SQL 注入,如下所示:

$query = "INSERT INTO users (`FirstName`,`LastName`,`dob`,`SchoolName`,`Username`,`Pass`)VALUES(?,?,?,?,?,?)";
$query->bind_param("ssssss", $fname, $lname, $dob, $school, $username, $pass);

接下来..

你在打电话if(isset($_POST['register'])){

然而你<button type="submit" class="btn btn-primary" id="register">Sing Up</button>没有name="register">

因此它应该是:<button type="submit" class="btn btn-primary" id="register" name="register">Sing Up</button>

此外,您永远不应该将密码作为纯文本保存到数据库中。你应该运行password_hash($pass, PASSWORD_DEFAULT);它来加密提交到数据库的密码 - 在这样做之前,我建议你阅读文档。

于 2017-03-09T14:53:51.180 回答
-1

看来您没有发送$_POST['register']到您的register.php,因此if (isset($_POST['register']))将评估为 false 并且绝对不会发生任何事情。$_POST其他字段也是如此,如果你给你的输入字段一个name属性,你只会有值。

于 2017-03-09T14:45:34.803 回答
-1

改变

  <button type="submit" class="btn btn-primary" id="register">Sing Up</button>

  <button type="submit" class="btn btn-primary" id="register" name="register">Sing Up</button>

此外,对于所有其他 $_POST 变量,您需要使用该name字段而不是该id字段。

您的 SQL 查询也有一个杂散的逗号。将其更改为

  $query = "INSERT INTO users (FirstName,LastName,dob,SchoolName,Username,Pass)VALUES('$FirstName','$LastName','$Username','$dob', '$SchoolName','$Pass')";
于 2017-03-09T14:45:36.253 回答