Geen connectie met database
Ik ben op dit moment bezig met een frontpage van een simpele website. Hierin staat een login.php pagina en een registratie.php pagina. Volgens mij, heb ik de php gedeeltes van de login en registratie pagina redelijk in orde. Het enigste probleem is dat ik ze op geen enkele manier gelinkt kan krijgen aan mijn MySQL database in PHPMyAdmin. Hostname, username, password en database kloppen gewoon, volgens mij, en komen ook overeen met de name-attributes in mijn forms. Info van database waarmee gelinkt moet worden staat in apart config.php bestandje in de root van de website:
Code (php)
1
2
3
4
5
6
2
3
4
5
6
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$dbname = 'test.users'; // test = databank in PHPMyAdmin en users = table. Beide apart geprobeerd en samen; ik kom er niet uit.
?>
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$dbname = 'test.users'; // test = databank in PHPMyAdmin en users = table. Beide apart geprobeerd en samen; ik kom er niet uit.
?>
Login(php & form):
<form action='home.php' method='post'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
<input type='submit' value='Login' />
</form>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
session_start();
if($_POST) {
require_once 'config.php'; // INFO STAAT DUS HIERBOVEN ^^^
$username = $_POST['username'];
$password = $_POST['password'];
$conn = mysql_connect($dbhost,$dbuser,$dbpass) // DIT LEEST HIJ DUS NIET (volgens mij...)
or die ('Error connecting to mysql');
mysql_select_db($dbname); // DIT OOK NIET
$query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s') AND password='%s'",
mysql_real_escape_string($username),
mysql_real_escape_string(md5($password)));
$result = mysql_query($query);
list($count) = mysql_fetch_row($result);
if($count == 1) {
$_SESSION['authenticated'] = true;
$_SESSION['username'] = $username;
$query = sprintf("UPDATE users SET last_login = NOW() WHERE UPPER(username) = UPPER('%s') AND password = '%s'",
mysql_real_escape_string($username),
mysql_real_escape_string(md5($password)));
mysql_query($query);
$query = sprintf("SELECT is_admin FROM users WHERE UPPER(username) = UPPER('%s') AND password='%s'",
mysql_real_escape_string($username),
mysql_real_escape_string(md5($password)));
$result = mysql_query($query);
list($is_admin) = mysql_fetch_row($result);
if($is_admin == 1) {
header('Location:admin.php');
} else {
header('Location:home.php');
}
} else { ?>
session_start();
if($_POST) {
require_once 'config.php'; // INFO STAAT DUS HIERBOVEN ^^^
$username = $_POST['username'];
$password = $_POST['password'];
$conn = mysql_connect($dbhost,$dbuser,$dbpass) // DIT LEEST HIJ DUS NIET (volgens mij...)
or die ('Error connecting to mysql');
mysql_select_db($dbname); // DIT OOK NIET
$query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s') AND password='%s'",
mysql_real_escape_string($username),
mysql_real_escape_string(md5($password)));
$result = mysql_query($query);
list($count) = mysql_fetch_row($result);
if($count == 1) {
$_SESSION['authenticated'] = true;
$_SESSION['username'] = $username;
$query = sprintf("UPDATE users SET last_login = NOW() WHERE UPPER(username) = UPPER('%s') AND password = '%s'",
mysql_real_escape_string($username),
mysql_real_escape_string(md5($password)));
mysql_query($query);
$query = sprintf("SELECT is_admin FROM users WHERE UPPER(username) = UPPER('%s') AND password='%s'",
mysql_real_escape_string($username),
mysql_real_escape_string(md5($password)));
$result = mysql_query($query);
list($is_admin) = mysql_fetch_row($result);
if($is_admin == 1) {
header('Location:admin.php');
} else {
header('Location:home.php');
}
} else { ?>
<span style='color:red'>Error: that username and password combination does not match any currently within our database.</span>
Registratie(php & form)
<form method='post' action='register.php'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
Confirm Password: <input type='password' name='confirm' /><br />
<input type='submit' value='Register!' />
</form>
Code (php)
<span style='color:red'>Error: Passwords do not match!</span>
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
<?php } else {
require_once 'config.php';
$conn = mysql_connect($dbhost,$dbuser,$dbpass) // Zelfde verhaal weer...
or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s')",
mysql_real_escape_string($_POST['username']));
$result = mysql_query($query);
if($result) { ?>
require_once 'config.php';
$conn = mysql_connect($dbhost,$dbuser,$dbpass) // Zelfde verhaal weer...
or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s')",
mysql_real_escape_string($_POST['username']));
$result = mysql_query($query);
if($result) { ?>
<span style='color:red'>Error: that username is taken.</span>
Code (php)
1
2
3
4
5
6
2
3
4
5
6
<?php } else {
$query = sprintf("INSERT INTO users(username,password) VALUES ('%s','%s');",
mysql_real_escape_string($_POST['username']),
mysql_real_escape_string(md5($password)));
mysql_query($query);
?>
$query = sprintf("INSERT INTO users(username,password) VALUES ('%s','%s');",
mysql_real_escape_string($_POST['username']),
mysql_real_escape_string(md5($password)));
mysql_query($query);
?>
<span style='color:green'>Congratulations, you registered successfully!</span>
Voor de duidelijkheid afbeelding van m'n database;
Als ik probeer mezelf te registreren krijg ik de volgende foutmelding:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'username'@'localhost' (using password: YES) Error connecting to mysql
Ik hoop dat iemand mij hiermee verder kan helpen :)
Gewijzigd op 17/10/2013 15:37:41 door Hetty van der Wal
$dbname = 'test';
Hetty van der Wal op 17/10/2013 15:33:51:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'username'@'localhost' (using password: YES) Error connecting to mysql
De gebruiker 'username' bestaat niet of het password is niet goed.