Reputation: 279
I have created a login system for a website however I receive the following error message:
Warning: PDOStatement::execute(): SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in C:\wamp\www\Bitev3\core\classes\users.php on line 18 when trying to log the user in.
login php file
<?php
require 'core/init.php';
$general->logged_in_protect();
if (empty($_POST) === false) {
$email = trim($_POST['email']);
$password = trim($_POST['password']);
if (empty($email) === true || empty($password) === true) {
$errors[] = 'Sorry, but we need your email and password.';
} else if ($users->email_exists($email) === false) {
$errors[] = 'Sorry that email doesn\'t exists.';
} else if ($users->email_confirmed($email) === false) {
$errors[] = 'Sorry, but you need to activate your account.
Please check your email.';
} else {
$login = $users->login($email, $password);
if ($login === false) {
$errors[] = 'Sorry, that email/password is invalid';
}else {
// email/password is correct and the login method of the $users object returns the user's id, which is stored in $login.
$_SESSION['UserID'] = $login; // The user's id is now set into the user's session in the form of $_SESSION['id']
#Redirect the user to home.php.
header('Location: home.php');
exit();
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/style.css" >
<title>Login</title>
</head>
<body>
<div id="container">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="register.php">Register</a></li>
<li><a href="login.php">Login</a></li>
</ul>
<h1>Login</h1>
<?php if(empty($errors) === false){
echo '<p>' . implode('</p><p>', $errors) . '</p>';
}
?>
<form method="post" action="">
<h4>Email:</h4>
<input type="text" name="email">
<h4>Password:</h4>
<input type="password" name="password">
<br>
<input type="submit" name="submit">
</form>
</div>
</body>
</html>
init php file
<?php
#starting the users session
session_start();
require 'connect/database.php';
require 'classes/users.php';
require 'classes/general.php';
$users = new Users($db);
$general = new General();
$errors = array();
database php file
<?php
$config = array(
'host' => 'locathost',
'username' => 'root',
'password' => 'pass123',
'dbname' => 'bitev2'
);
$db =new PDO ('mysql:host'. $config['host'] . ':dbname=' . $config['dbname'], $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
general php file
class General{
#Check if the user is logged in.
public function logged_in () {
return(isset($_SESSION['UserID'])) ? true : false;
}
#if logged in then redirect to home.php
public function logged_in_protect() {
if ($this->logged_in() === true) {
header('Location: home.php');
exit();
}
}
#if not logged in then redirect to index.php
public function logged_out_protect() {
if ($this->logged_in() === false) {
header('Location: index.php');
exit();
}
}}
user php file
<?php
class Users{
private $db;
public function __construct($database) {
$this->db = $database;
}
public function email_exists($email) {
$query = $this->db->prepare("SELECT COUNT(`UserID`) FROM `users` WHERE `Email`= ?");
$query->bindValue(1, $email);
try{
$query->execute();
$rows = $query->fetchColumn();
if($rows == 1){
return true;
}else{
return false;
}
} catch (PDOException $e){
die($e->getMessage());
}
}
public function register($firstname, $lastname, $gender, $password, $email){
$time = time();
$ip = $_SERVER['REMOTE_ADDR'];
$email_code = sha1($firstname + $lastname + microtime());
$password = sha1($password);
$query = $this->db->prepare("INSERT INTO `users` (`FirstName`, `LastName`, `Email`, `Password`, `Gender`, `ip`, `time`, `Email_code`) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ");
$query->bindValue(1, $firstname);
$query->bindValue(2, $lastname);
$query->bindValue(3, $email);
$query->bindValue(4, $password);
$query->bindValue(5, $gender);
$query->bindValue(6, $ip);
$query->bindValue(7, $time);
$query->bindValue(8, $email_code);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
public function activate($email, $email_code) {
$query = $this->db->prepare("SELECT COUNT(`UserID`) FROM `users` WHERE `Email` = ? AND `Email_code` = ? AND `confirmed` = ?");
$query->bindValue(1, $email);
$query->bindValue(2, $email_code);
$query->bindValue(3, 0);
try{
$query->execute();
$rows = $query->fetchColumn();
if($rows == 1){
$query_2 = $this->db->prepare("UPDATE `users` SET `confirmed` = ? WHERE `Email` = ?");
$query_2->bindValue(1, 1);
$query_2->bindValue(2, $email);
$query_2->execute();
return true;
}else{
return false;
}
} catch(PDOException $e){
die($e->getMessage());
}
}
public function login($email, $password) {
$query = $this->db->prepare("SELECT `Password`, `UserID` FROM `users` WHERE `Email` = ?");
$query->bindValue(1, $email);
try{
$query->execute();
$data = $query->fetch();
$stored_password = $data['Password'];
$id = $data['UserID'];
if($stored_password === sha1($password)){
return $id;
}else{
return false;
}
}catch(PDOException $e){
die($e->getMessage());
}
}
public function email_confirmed($email) {
$query = $this->db->prepare("SELECT COUNT(`UserID`) FROM `users` WHERE `Email`= ? AND `confirmed` = ?");
$query->bindValue(1, $email);
$query->bindValue(2, 1);
try{
$query->execute();
$rows = $query->fetchColumn();
if($rows == 1){
return true;
}else{
return false;
}
} catch(PDOException $e){
die($e->getMessage());
}
}
public function userdata($id) {
$query = $this->db->prepare("SELECT * FROM `users` WHERE `UserID`= ?");
$query->bindValue(1, $id);
try{
$query->execute();
return $query->fetch();
} catch(PDOException $e){
die($e->getMessage());
}
}
public function get_users() {
$query = $this->db->prepare("SELECT * FROM `users` ORDER BY `time` DESC");
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
return $query->fetchAll();
}
}
Upvotes: 2
Views: 7047
Reputation: 1733
$db =new PDO ('mysql:host='. $config['host'] . ';:dbname=' . $config['dbname'], $config['username'], $config['password']);
Upvotes: -2
Reputation: 1451
You have a typo that may cause some of the issue:
$config = array(
'host' => 'locathost',
'username' => 'root',
'password' => 'pass123',
'dbname' => 'bitev2'
);
Try changing locathost to localhost.
Upvotes: 4
Reputation: 466
$db =new PDO ('mysql:host'. $config['host'] . ':dbname=' . $config['dbname'], $config['username'], $config['password']);
This needs to be:
$db =new PDO ('mysql:host='. $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);
Upvotes: 2
Reputation: 5754
typo ?
$db = new PDO ('mysql:host'. $config['host'] . ':dbname=' . $config['dbname'], $config['username'], $config['password']);
should be
$db = new PDO ('mysql:host='. $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);
Upvotes: 3