注册,包括后失败


register including post fail

我正在尝试建立一个带有注册等功能的登录系统。现在,我使用一个表格和方法"张贴"进行注册。现在它失败了,我认为是通过邮件发送输入。你能帮我修一下吗?这是其中涉及的代码:

上面!doctype

<?php
include('connect.php');
// If the values are posted, insert them into the database.
if (isset($_POST["username"]) && isset($_POST["password"])){
    $username = $_POST["username"];
    $email = $_POST["email"];
    $password = $_POST["password"];
    $query = "INSERT INTO `user` (username, password, email) VALUES ($username, $password, $email)";
    $result = mysqli_query($query);
    if($result){
        $msg = "User Created Successfully.";
    }
else
  {echo "fail";}
}
?>

形式:

<div class="register-form">
<?php
if(isset($msg) & !empty($msg)){
    echo $msg;
}
?>
<h1>Registreer</h1>
<form action="" method="POST">
<p><label>User Name : </label>
<input id="username" type="text" name="username" placeholder="username" /></p>
<p><label>E-Mail&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </label>
 <input id="password" type="email" name="email" required placeholder="name@email.com" /></p>
 <p><label>Password&nbsp;&nbsp; : </label>
 <input id="password" type="password" name="password" placeholder="password" /></p>
<a class="btn" href="login.php">Login</a>
<input class="btn register" type="submit" name="submit" value="Registreer" />
</form>
</div>    

connect.php

<?php
$servername = "localhost";
$username = "sqluser";
$password = "Welkom01!";
$dbname = "users";
$connection = mysqli_connect($servername, $username, $password);
if (!$connection){
die("Database Connection Failed". mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, $dbname);
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
?>

提前谢谢。

根据您最初发布的问题,如果没有在新编辑的问题下将其标记为编辑,是否有人想知道答案的原因。


因为我们很可能处理字符串

VALUES ($username, $password, $email)

需要用引号括起来:

VALUES ('$username', '$password', '$email')

您还需要将DB连接传递到查询$result = mysqli_query($query);

编辑:(您在之后添加了数据库连接代码)来自您的原始帖子

由于您没有显示您的数据库连接是什么,这将类似于

$result = mysqli_query($connection,$query);

加上,将or die(mysqli_error($connection))添加到mysqli_query()

if(isset($msg) & !empty($msg)){中还有一个丢失的&,应读作if(isset($msg) && !empty($msg)){


  • 但是,您当前的代码对SQL注入是开放的
    使用准备好的语句,或将PDO与准备好的声明一起使用它们更安全

密码

我还注意到,您可能将密码存储为纯文本。不建议这样做。

使用以下选项之一:

  • CRYPT_BLOWFISH
  • crypt()
  • bcrypt()
  • scrypt()
  • 在OPENWALL上
  • PBKDF2
  • PHP.net上的PBKDF2
  • PHP 5.5的password_hash()函数
  • 兼容性包(如果PHP<5.5)https://github.com/ircmaxell/password_compat/

其他链接:

  • PBKDF2用于PHP