[OPGELOST] OOP User registratie lukt niet
Ik heb een Database class overgenomen van de OOP handleiding van Blanche en een User class geschreven.
Wanneer ik een User probeer te registreren krijg ik de volgende melding (let even niet op de url en regelnummers, deze kloppen niet):
Notice: Undefined variable: db in /public/sites/www.url.nl/class/user.class.php on line 22
Fatal error: Call to a member function query() on a non-object in /public/sites/www.url.nl/class/user.class.php on line 22
Dit is het script (zonder het formulier, dat er natuurlijk wel is):
global.inc.php
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
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
<?php
// Show errors
error_reporting(E_ALL);
ini_set('display_errors',true);
// ob
ob_start();
// Start session
session_start();
#===== Database Settings =====#
define('DB_HOST', '****'); // Database host
define('DB_USER', '****'); // Database username
define('DB_PASS', '****'); // Database password
define('DB_DB', '****'); // Database name
// Pagina's includen
include(BASE_PATH.'/inc/includes.inc.php');
// Verbinding met de database maken
$db = new Database();
$db->connect(DB_HOST, DB_USER, DB_PASS);
$db->selectDatabase(DB_DB);
?>
// Show errors
error_reporting(E_ALL);
ini_set('display_errors',true);
// ob
ob_start();
// Start session
session_start();
#===== Database Settings =====#
define('DB_HOST', '****'); // Database host
define('DB_USER', '****'); // Database username
define('DB_PASS', '****'); // Database password
define('DB_DB', '****'); // Database name
// Pagina's includen
include(BASE_PATH.'/inc/includes.inc.php');
// Verbinding met de database maken
$db = new Database();
$db->connect(DB_HOST, DB_USER, DB_PASS);
$db->selectDatabase(DB_DB);
?>
Database.class.php
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/*****
*
* Class - Database
*
* Deze class handelt de communicatie met de MySQL Database af.
* De methods kunnen verder worden uitgebreid.
*
* ------------------------------------------------------------
*
* Gebruik:
*
* $result = $db->query("SELECT username FROM users");
*
* while($row = $db->fetchAssoc($result)) {
* echo($row['username']);
* }
*
*****/
class Database {
private $_link;
public function connect($server='', $username='', $password='', $new_link=true, $client_flags=0) {
$this->_link = mysql_connect($server, $username, $password, $new_link, $client_flags);
}
public function selectDatabase($db){
return mysql_select_db($db, $this->_link);
}
public function error() {
return mysql_error($this->_link);
}
public function errno() {
return mysql_errno($this->_link);
}
public function escape($string) {
return mysql_real_escape_string($string, $this->_link);
}
public function query($query) {
return mysql_query($query, $this->_link);
}
public function fetchArray($result, $array_type = MYSQL_BOTH) {
return mysql_fetch_array($result, $array_type);
}
public function fetchRow($result) {
return mysql_fetch_row($result);
}
public function fetchAssoc($result) {
return mysql_fetch_assoc($result);
}
public function fetchObject($result) {
return mysql_fetch_object($result);
}
public function numRows($result) {
return mysql_num_rows($result);
}
public function close() {
return mysql_close($this->_link);
}
}
?>
/*****
*
* Class - Database
*
* Deze class handelt de communicatie met de MySQL Database af.
* De methods kunnen verder worden uitgebreid.
*
* ------------------------------------------------------------
*
* Gebruik:
*
* $result = $db->query("SELECT username FROM users");
*
* while($row = $db->fetchAssoc($result)) {
* echo($row['username']);
* }
*
*****/
class Database {
private $_link;
public function connect($server='', $username='', $password='', $new_link=true, $client_flags=0) {
$this->_link = mysql_connect($server, $username, $password, $new_link, $client_flags);
}
public function selectDatabase($db){
return mysql_select_db($db, $this->_link);
}
public function error() {
return mysql_error($this->_link);
}
public function errno() {
return mysql_errno($this->_link);
}
public function escape($string) {
return mysql_real_escape_string($string, $this->_link);
}
public function query($query) {
return mysql_query($query, $this->_link);
}
public function fetchArray($result, $array_type = MYSQL_BOTH) {
return mysql_fetch_array($result, $array_type);
}
public function fetchRow($result) {
return mysql_fetch_row($result);
}
public function fetchAssoc($result) {
return mysql_fetch_assoc($result);
}
public function fetchObject($result) {
return mysql_fetch_object($result);
}
public function numRows($result) {
return mysql_num_rows($result);
}
public function close() {
return mysql_close($this->_link);
}
}
?>
User.class.php
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
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
<?php
/*****
*
* User class
*
* -> Usergegevens opvragen
* -> Nieuwe user registeren
*
*****/
class User{
private $_id;
private $_db;
public function __construct($id, $db){
$this->_id = $id;
$this->_db = $db;
}
public function register($p){
$res = $db->query("INSERT INTO user (email,password) VALUES
('".mysql_real_escape_string($p['email'])."', '".mysql_real_escape_string($p['password'])."')");
if(!$res){
echo 'Er is een fout opgetreden tijdens het registreren: '.mysql_error();
} else {
// Doorsturen om dubbele registratie door F5 te voorkomen
echo '<h1>U bent succesvol geregistreerd!</h1>';
// header("location:http://www.url.nl/template.php");
}
}
}
?>
/*****
*
* User class
*
* -> Usergegevens opvragen
* -> Nieuwe user registeren
*
*****/
class User{
private $_id;
private $_db;
public function __construct($id, $db){
$this->_id = $id;
$this->_db = $db;
}
public function register($p){
$res = $db->query("INSERT INTO user (email,password) VALUES
('".mysql_real_escape_string($p['email'])."', '".mysql_real_escape_string($p['password'])."')");
if(!$res){
echo 'Er is een fout opgetreden tijdens het registreren: '.mysql_error();
} else {
// Doorsturen om dubbele registratie door F5 te voorkomen
echo '<h1>U bent succesvol geregistreerd!</h1>';
// header("location:http://www.url.nl/template.php");
}
}
}
?>
template.php
Code (php)
Toevoeging op 23/12/2010 16:01:59:
Oplossing gevonden:
$db moest zijn: $this->_db
Gewijzigd op 23/12/2010 16:02:16 door The Ultimate
Er zijn nog geen reacties op dit bericht.