Undefined Index Error

I am trying to get my login for my application to work however it keeps refreshing to index page. Whilst doing some debugging it seems the problem is with one line in my controller -
//if email and password matches one in the database
[php]$view->LoginDataSet = $LoginDataSet->fetchLoginDetails($_POST[‘Email’], $_POST[‘Password’]);[/php]

I am using MVC and PDO too for my application.

Model

LoginData.php

[php]<?php

class LoginData {

protected $Name, $Email, $Password;

public function __construct($dbrow) {
    $this->Name = $dbrow['Name'];
    $this->Email = $dbrow['Email'];
    $this->Password = $dbrow['Password'];
}

 function getName() {
    return $this->Name;
}

function getEmail() {
    return $this->Email;
}

function getPassword() {
    return $this->Password;
}

function logout() {
    $_SESSION = array();
    session_destroy();
}

}

[/php]

LoginDataSet.php

[php]<?php

require_once(‘Model/Database.php’);
require_once(‘Model/LoginData.php’);

class LoginDataSet {

protected $_dbHandle, $_dbInstance = null;

public function __construct() {
    $this->_dbInstance = Database::getInstance();
    $this->_dbHandle = $this->_dbInstance->getdbConnection();
}

public function fetchLoginDetails($Email, $Password) {

    $Password = crypt($Password, $Email);

    $sqlQuery = "SELECT * FROM users WHERE Email=:u AND Password=:p"; //basic SQL Query
	//var_dump($sqlQuery); die();
    $statement = $this->_dbHandle->prepare($sqlQuery); //Prepare PDO statement
    //SQL Injection
    $statement->execute(array(
        ':u' => $Email,
        ':p' => $Password
    )); //Executes PDO statement
    $dataSet = [];
    while ($row = $statement->fetch()) { //Fetches the next row matching the query
        $dataSet[] = new LoginData($row);
    }

    return $dataSet;
}

public function fetchProfileDetails($Name) {
    $sqlQuery = "SELECT * user WHERE Name='" . $Name . "'";

    $statement = $this->_dbHandle->prepare($sqlQuery); //Prepare PDO statement
    $statement->execute(); //Executes PDO statement

    $dataSet = [];
    while ($row = $statement->fetch()) { //Fetches the next row matching the query
        $dataSet[] = new LoginData($row);
    }

    return $dataSet;
}

}

?>[/php]

View

[php]<!DOCTYPE>

<?php if (!isset($_SESSION)) { session_start(); } ?> My Comics
Toggle navigation Comic Collection
<?php if (isset($_SESSION['Email']) && $_SESSION['Email'] <> ''): ?> <?php endif; ?> <?php if (isset($_SESSION['Email'])): ?> <?php endif; ?> <?php if (isset($_SESSION['Email']) && $_SESSION['Email'] <> ''): ?>
<?php if (isset($_SESSION['Email'])): ?> <?php if ($_SESSION['Email'] != "") echo "Welcome " . $_SESSION['Email']; ?> <?php endif; ?>
<?php else: ?>
Login <?php endif; ?>

[/php]

Controller

login.php

[php]<?php

//session start will always be an email
session_start();
$view = new stdClass();
$view->pageTitle = ‘LoggedIn’;

require_once (‘Model/LoginDataSet.php’);

//if submit is pressed
if (isset($_POST[‘submit’])) {
//check the email and password against the one in the database.
$LoginDataSet = new LoginDataSet();
//if email and password matches one in the database
$view->LoginDataSet = $LoginDataSet->fetchLoginDetails($_POST[‘Email’], $_POST[‘Password’]);
//get the variables below using the functions of logindataset
if (count($view->LoginDataSet) == 1) {
$_SESSION[‘Email’] = $_POST[‘Email’];;
//continue on to profile page
header(“Location:home.php”);
} else {
//if incorrect return to index page with error
$_SESSION[‘error’] = “logindetails”;
header(“Location:index.php”);
}
}

require_once(‘View/home.phtml’);
[/php]

How do I rectify the problem as I have been going through books and other forums however nothing is helping. I am new to PHP and more towards ASP.net . Any help would be appreciated as I have come to a dead end.

name=“password” == $_POST[‘password’]

Hi astonecipher, Where would i put that in my code ?

Not actually something to put… Just as in .net,

password is not the same as Password. You are using $_POST[‘Password’], but the index is for password.

You also need to give your prepared statement another look. You are loosing it’s benefit when concatenating the string into the statement.

Sorted the problem out thanks for your help

Sponsor our Newsletter | Privacy Policy | Terms of Service