Regina Shepherd Riddle Regina Shepherd Riddle - 7 months ago 27
SQL Question

PHP MYSQL query is not passing the array into my variable

Here is my query and it's coming up with $results as NULL when I do a var_dump:

function connect($loginName) {
$password = (isset($_SESSION['password']));
$loginName = (isset($_SESSION['loginName']));

global $db;
$query = "SELECT email, level, password FROM members WHERE email = '$loginName'";
$result = $db->query($query);
$results = $result->fetch(PDO::FETCH_ASSOC);
return $results;
}


After attempting changes suggested it is giving me these two errors in my welcome form:

Errors in WElcome section of my website

FUNCTIONS PAGE:

<?php
require('database_connection.php');
//include('index.php')
//session_start();
if ((isset($_POST['loginName'])) && (isset($_POST['password']))){
$_SESSION['loginName'] = $_POST['loginName'];
$_SESSION['password'] = password_hash($_POST['password'], PASSWORD_BCRYPT);

$password = isset($_SESSION['password']) ? $_SESSION['password'] : "";
$loginName = isset($_SESSION['loginName']) ? $_SESSION['loginName'] : "";


// Connecting to the database
}

function connect($loginName) {

global $db;
$query = "SELECT email, level, password FROM members WHERE email = '$loginName'";
$result = $db->query($query);
$results = $result->fetch(PDO::FETCH_ASSOC);
return $results;
}


//Login

function login($loginName, $password) {
$results = connect($loginName);
if ((isset($_SESSION['loginName'])) === $results['email'] && (isset ($_SESSION['password'])) === $results['password'])
{

if ( 'a' === $results['level'] ) {
$level = "Administrator";
}
elseif ( 'm' === $results['level'] ) {
$level = "Member";
}
else $level = '?';

if ($level === "Administrator"){
header('Location: /tires/admin/home.php');
exit();
}elseif ($level === "Member"){
header('Location: /tires/member/home.php');
exit();
}
include('logoutform.php');}



else {

echo "Sorry. You are not in our database";

}};



//Trying to verify the password

if ((isset($_POST['loginName'])) && (isset($_POST['password']))){
$_SESSION['loginName'] = $_POST['loginName'];
$_SESSION['password'] = password_hash($_POST['password'], PASSWORD_BCRYPT);
}
$password = (isset($_SESSION['password']));
$loginName = (isset($_SESSION['loginName']));
$hash = '';

global $db;
$h = $db->prepare("SELECT password FROM members WHERE email = '$loginName'");
$h->execute();
$hashing = $h->fetchAll();
foreach ($hashing as $hash) {
return $hash['password'];
if($hashing) // will return true if succefull else it will return false
{
echo 'Query is working';// code here for true
};
};

if (password_verify($password, $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
};




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


function get_name($results) {
$name = preg_split("/@/", $results['email']);
$name = ucfirst($name[0]);
return $name;
};



function allowed_get_params($allowed_params=[]) {
$allowed_array = [];
foreach($allowed_params as $param) {
if(isset($_GET[$param])) {
$allowed_array[$param] = $_GET[$param];
} else {
$allowed_array[$param] = NULL;
}
}
return $allowed_array;
};

$get_params = allowed_get_params(['loginName', 'password']);


?>


AND, This is the login (logout really) form:

<form method="post" id="logoutform.php">

<fieldset>
<legend>Logout</legend>

<?php
//include('includes/functions.php');
//if ( $_SESSION['loginName'] === $result['email'] && $_SESSION['password'] === $result['password']) {
echo "Welcome, ";
echo "$level, ";
echo get_name($results);
// print_r($_POST);
print '<br />';
//}
// else{
//var_dump($result);
///}
?> <br /> <br /> <br />
<input type="submit" name="action" value="logout" />

</fieldset></form>


THe LOGIN FORM is this one:

<form action="home.php" method="post" id="loginform.inc.html">
<fieldset>

<legend>Login</legend> <label for="loginName" class="loginName">Username:</label>
<input id="loginName" name="loginName" type="text" value="<?php echo (isset($loginName['loginName'])); ?>"/>
<label for="password" class="loginName">Password:</label>
<input id="password" name="password" type="password" value="<?php echo (isset($password['password'])); ?>" />
<input type="submit" name="submit" value="login" />
</fieldset>
</form>

Answer

Change

$password = (isset($_SESSION['password']));
$loginName = (isset($_SESSION['loginName']));

to

$password = isset($_SESSION['password']) ? $_SESSION['password'] : "";
$loginName = isset($_SESSION['loginName']) ? $_SESSION['loginName'] : "";
Comments