I'm in the basics of learning to code in OOP. So far I've tried simple input/output and succeeded but am now trying to move onto something still rather basic but a step up from previous attempts.
Anyway, I'm writing a login script connecting to a PDO database. I've tested all input/outputs which work fine, but the actual selecting the user info from the database just doesn't work. I'm getting this error:
Fatal error: Call to undefined function getUser() in C:\xampp\htdocs\classes\loginclass.php on line 25
If I'm going about this the wrong way, please let me know as I'm just testing out more efficient ways of coding which is why I'm going for OOP rather than procedural.
index.php
<form method = "post" action="login.php">
<label>Username</label><input type="text" name="username" id="username"><br />
<label>Password</label><input type="password" name="password">
<label>Password</label><input type="password" name="password2">
<input type="submit" value="submit" name="submit">
</form>
login.php
<?php
//Get data from the form
if (isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
}
//Include the class files
include "classes/loginclass.php";
//Instantiate the login function
$login = new Login();
//Set the password variables in the class to match the password passed from the form
$login->password = $password;
$login->password2 = $password2;
//Do it!
$login->logMeIn();
?>
loginclass.php
<?php
class Login {
//Declare variables
public $username;
public $password;
public $password2;
//Start login function
function logMeIn(){
//Check if passwords match
if ($this->password == $this->password2){ //Passwords match, now include db.php and check if connection to database is successful
include ("classes/db.php");
if ($success = true){
//echo "Connected to db via PDO...";
include ("classes/userclass.php");
$user = new getUser();
$user->table = "users";
$user->username = $username;
$user->where = "username";
//Do it!
getUserDetails();
}else {
die("Not connected to the database");
}
}else {
die("Your passwords do not match");
}
}
}
?>
db.php
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
$username = "***";
$password = "***";
$id = 1;
try {
$conn = new PDO('mysql:host=localhost;dbname=user', $username, $password);
$success = true;
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
userclass.php
<?php
class getUser {
//Declare variables
public $username;
//Get user details from database
function getUserDetails(){
include ("classes/db.php");
$stmt = $conn->prepare('SELECT * FROM $this->table WHERE $this->where = :$this->$username');
$stmt->execute(array('username' => $username));
while($row = $stmt->fetch()) {
print_r($row);
}
}
}
?>
Thanks in advance for any help/advise you give me.