[英]mysqli registration script, check if user or email exsist
Check accepted answer if you have a simular issue. 如果您有类似问题,请检查接受的答案。
First of all, i know there are a few simular questions like this but non of them where able to help me. 首先,我知道有一些类似的问题,但是没有一个可以帮助我。
I'm trying to check if username or email is already in the database, i already have them set to unique in the database, but want this so i can show seperate errors for username / email. 我正在尝试检查用户名或电子邮件是否已在数据库中,我已经将它们设置为数据库中的唯一名称,但是想要这样做,以便我可以显示用户名/电子邮件的单独错误。
Right now it just outputs a white page when trying to register and i have spent hours trying to figure it out by myself.. any help will be highly appreciated. 现在,当您尝试注册时,它只会输出一个白页,而我已经花费了数小时试图自己弄清楚它。.任何帮助将不胜感激。
I added comments around the problematic code i cant get to work. 我添加了有关无法正常工作的有问题代码的注释。
This is my code: 这是我的代码:
<?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'");
}}}
?>
I use PDO and I have this code which checks to see if a username has already been taken 我使用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!');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.