如果您有模拟问题,请检查已接受的答案
首先,我知道有一些类似的问题,但没有一个能帮助我
我正在尝试检查用户名或电子邮件是否已经在数据库中,我已经将它们设置为在数据库中唯一,但希望这样,我可以显示用户名/电子邮件的单独错误。
现在,它在尝试注册时只输出一个白色页面,我花了几个小时试图自己解决这个问题。。任何帮助都将不胜感激。
我在有问题的代码周围添加了注释,我无法开始工作。
这是我的代码:
<?php
// Include database connection and functions here.
include 'db_connect.php';
include 'functions.php';
// The hashed password from the form
$password = $_POST['p'];
// Create a random salt
$random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
// Create salted password (Careful with the chilli)
$username = $_POST['username'];
$email = $_POST['email'];
//Start of problematic code
$check_stmt = $mysqli->prepare("SELECT * FROM members WHERE username=? OR email=?");
$check_stmt->bind_param('ss', $username, $email);
$check_stmt->execute();
$check_result = $check_stmt->get_result();
while ($row = $check_result->fetch_assoc()) {
if ($row->num_rows > 0)
{
if ($row['username'] > '')
{
header("Location: '..'..'..'?RegUserExsist=1'");
}
else if ($row['email'] > '')
{
header("Location: '..'..'..'?RegEmailExsist=1'");
}
}
//end of problematic code
else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
header("Location: '..'..'..'?RegInvalidEmail=1'");
}
else if (empty($username))
{
header("Location: '..'..'..'?RegInvalidUser=1'");
}
else if (empty($password))
{
header("Location: '..'..'..'?RegInvalidPass=1'");
}
else
{
$password2 = hash('sha512', $password.$random_salt);
if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)"))
{
$insert_stmt->bind_param('ssss', $username, $email, $password2, $random_salt);
// Execute the prepared query.
$insert_stmt->execute();
header("Location: '..'..'..'?success=1'");
}
else
{
header("Location: '..'..'..'?regfailed=1'");
}}}
?>
我使用PDO,我有这个代码,它检查用户名是否已经被使用
<?php
/* Create a new PDO object with database connection parameters */
$dbh = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.'', DB_USER, DB_PASS);
// Create statement object
$stmt = $dbh->prepare("SELECT username FROM users WHERE username = :username");
$stmt->bindValue(":username", $user);
$stmt->execute();
if($stmt->rowCount() != 0) {
$this->setError('<span class="bold">Username</span> you have selected has already been used by another member in our database. Please choose a different Username!');
}