-
Apprentice
Need some help with a register page for WoW BFA
So i've created the following code, but it seems that i cannot connect to the database..Can someone please have a look at the code?
Error
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in C:\Website\htdocs\include\db.php on line 7
Failed to connect to MySQL - Reason: Access denied for user 'root'@'localhost' (using password: NO)
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="css/style.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Nexus Realms - Register</title>
<?php
//database
include("include/db.php");
?>
<?php
// Startvariabelen
$regist_array = [];
$regist_array2 = [];
$errors = [];
$error_count = 0;
// saves information towards the variables.
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = ($_POST['username']);
$sha_pass_hash = ($_POST['sha_pass_hash']);
$email = ($_POST['email']);
$reg_mail = ($_POST['reg_mail']);
// checks if the email address is used before.
$query = $pdo->prepare("SELECT email FROM account WHERE email = :email");
$query->execute(array(':email' => $email));
$result = $query->fetchAll(PDO::FETCH_OBJ);
//error shown if the email address is found.
if (count($result)) {
$errors[] = 'This email address is already in use.';
$error_count = 1;
}
// error if the field is left empty.
elseif (empty($username)) {
$errors[] = 'This username is already in use.';
$error_count = 1;
}
// error if the field is left empty. also checks if there are any weird letters and stuff.
elseif (!ctype_alpha(str_replace(array(' ', "'", '-'), '', $username)) && !empty($username)) {
$errors[] = 'This username is invalid.';
$error_count = 1;
$username = '';
}
// checks if a valid email address is used.
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'This email address is invalid.';
$error_count = 1;
$email = '';
}
// error if the field is left empty.
elseif (empty($sha_pass_hash)) {
$errors[] = 'Please fill in a password.';
$error_count = 1;
}
else{
//If everything checks out, an array will be made.
$regist_array[0] = $_POST['username'];
$regist_array[1] = $_POST['email'];
$regist_array[2] = $_POST['reg_mail'];
// password will be hashed.
$regist_array[3] = $_POST['sha_pass_hash'];
$regist_array[4] = 1;
// Query with the array will be executed.
$query = "INSERT INTO account (username, email, reg_mail, sha_pass_hash) VALUES (?, ?, ?, ?)";
$stmt = $pdo->prepare($query);
$stmt->execute($regist_array);
// fills the array for the prepared statement.
$regist_array2[0] = $_POST['email'];
$regist_array2[1] = $_POST['sha_pass_hash'];
$regist_array2[2] = 1;
// Query with the array will be executed.
$query2 = "INSERT INTO battlenet_accounts (email, sha_pass_hash) VALUES (?, ?)";
$stmt2 = $pdo->prepare($query2);
$stmt2->execute($regist_array2);
print "You're registered! You can now use your email address to log in to the game!";
}
}
if($error_count > 0) {
echo '<div class="alert alert-error"><span>ERROR</span><ul class="no-padding">';
foreach ($errors as $error) {
echo '<li>'.$error.'</li>';
} echo '</ul></div>';
}
?>
<form class="" method="post" action="register.php">
<h2>Register</h2>
<div class="col-md-4">
<!-- username -->
<label for="username">Username</label>
<input name="username" type="text" id="username"><br>
<!-- password -->
<label for="sha_pass_hash">Password</label>
<input name="sha_pass_hash" type="password" id="sha_pass_hash" required><br>
<!-- email -->
<label for="email">Mail address</label>
<input name="email" type="email" id="email" required autofocus><br>
<!-- email -->
<label for="reg_mail">Re-type mail address</label>
<input name="reg_mail" type="reg_mail" id="reg_mail" required autofocus><br>
<button id="RegisButton" class="btn btn-lg btn-primary btn-block" type="submit" name="submit">Register</button>
</div>
</form>
--------------------------------------------------------------------------------------------------------------
db.php
<?php
$host = "127.0.0.1";
$user = "root";
$pass = "";
$db = "auth";
$con=mysqli_connect($host,$user,$pass,$db);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL - Reason: " . mysqli_connect_error();
}
?>
-