我正在编码PHP system,我希望它检查用户名是否在注册页面中可用。我想避免数据库中的两个用户使用相同的用户名
我尝试了很多事情,但没有成功。
这是我的config.php:
// REGISTER USER
function register(){
// call these variables with the global keyword to make them available in
function
global $db, $errors, $username;
// receive all input values from the form. Call the e() function
// defined below to escape form values
$username = e($_POST['username']);
$password_1 = e($_POST['password_1']);
$password_2 = e($_POST['password_2']);
// form validation: ensure that the form is correctly filled
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password_1)) {
array_push($errors, "Password is required");
}
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
if (isset($_POST['user_type'])) {
$user_type = e($_POST['user_type']);
$query = "INSERT INTO users (username, user_type, password)
VALUES('$username', '$user_type', '$password')";
mysqli_query($db, $query);
$_SESSION['success'] = "New user successfully created!!";
header('location: dashboard.php');
}else{
$query = "INSERT INTO users (username, user_type, password)
VALUES('$username', 'user', '$password')";
mysqli_query($db, $query);
// get id of the created user
$logged_in_user_id = mysqli_insert_id($db);
$_SESSION['user'] = getUserById($logged_in_user_id); // put logged in user in session
$_SESSION['success'] = "You are now logged in";
header('location: dashboard.php');
}
}
}
答案 0 :(得分:0)
您应该添加一个新的错误句柄,检查用户名是否存在于数据库中。
var price = document.getElementById("price");
var reduction = document.getElementById("reduction");
var newPrice = document.getElementById("newPrice");
var priceToPay = document.getElementById("priceToPay");
price.addEventListener("keyup",myAnswer);
reduction.addEventListener("keyup",myAnswer);
priceToPay.addEventListener("change" ,myAnswer);
newPrice.addEventListener("change",myAnswer);
function myAnswer ()
{ // this part is used to display the value of the discount
newPrice.value = (Number(reduction.value) * Number(price.value)) / 100 + " £";
}
{
// this part does not work, but essentially is normally used to give me the total price of the item after the reduction has been applied.
priceToPay.value = Number(price.value) - Number(newPrice.value) + " £";
}