我要把数据插入我的数据库。数据被插入了,但是在数据被插入之后,我的结果应该是确定的,但我得到的是“注册不完成”。
有人能帮忙吗?
这是我的密码
<?php
require 'functions.php';
require 'lib/password.php';
if(isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
if(!empty($username) && !empty($password)){
$hash = password_hash($password, PASSWORD_BCRYPT);
$query = "INSERT INTO users (id, username, password) VALUES ('','".$username."','".$hash."')";
if($conn->query($query)===TRUE){
echo 'Ok';
} else{
echo 'Registration not complete';
}
}
}
?>
<form action="register.php" method="POST">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Register">
</form>发布于 2015-08-21 00:10:12
pdo::query()在成功时不会返回true。它返回一个PDOStatement对象。检查您的条件语句,您将看到它失败的原因:
if($conn->query($query)===TRUE)您的查询受SQL注入的影响。这是一个很好的例子,说明您何时应该使用pdo::准备()和PDOStatement::execute() (也就是准备好的语句)。您不应该在查询中直接使用用户输入(POST),在本例中是$username。
https://stackoverflow.com/questions/32130412
复制相似问题