使用PHP的Boostrap表单,MySQL不向数据库发送数据

时间:2017-03-09 14:39:22

标签: php html mysql forms boost-random

我一直在尝试调试这个问题多年了,不明白为什么不将数据发送到我的数据库。另外作为一个指示,我正在使用Boostrap,这个形式在一个模态中。有人可以帮帮我吗。我将包括我的html snipet以及php代码。

HTML CODE

func setPostedImage(image : UIImage) {

    let aspect = image.size.width / image.size.height

    aspectConstraint = NSLayoutConstraint(item: agentimage, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: agentimage, attribute: NSLayoutAttribute.Height, multiplier: aspect, constant: 0.0)

    agentimage.image = image
}

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>

4 个答案:

答案 0 :(得分: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>

然后你应该使用预准备语句来保存你的数据库中的数据,即使你在使用localhost,也不要在数据库中存储明文密码,因为不要存储密码,只需使用{存储密码的哈希值{1}}和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()的更多信息,请参阅手册:

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准备好的陈述。

答案 1 :(得分: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')";

更好你可以绑定params以防止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);这将加密提交给数据库的密码 - 在此之前,我建议您阅读文档。

答案 2 :(得分:0)

更改

  <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')";

答案 3 :(得分:-1)

您似乎没有向$_POST['register']发送register.php,因此if (isset($_POST['register']))将评估为false,绝对不会发生任何事情。 对于其他字段也是如此,如果您为输入字段指定$_POST属性,则name中只有值。