last day (15 days later) » 

7:40 AM
0
A: Using Aggregate String Functions in MySQL and PHP

NickUnfortunately MySQL doesn't sort enum values numerically, but instead by their string representation, otherwise you could just use MAX directly on the values. But you can sort by CASTing the values as integers e.g. SELECT staff.StaffID, MAX(CAST(role.Privilege AS UNSIGNED)) AS Privilege FROM job...

 
This is super, but this returns an error where Privilege is an undefined index?
 
@GoGoAllen sorry, forgot to give the value an alias. see my edit
 
Hmm, this still doesn't seem to work - doing this creates a blank page with no redirect. I added ((int)$gettier["Privilege"]) to both statements, however it still redirects to USERsuccess.
 
@GoGoAllen the db code works fine - see this demo dbfiddle.uk/…. PHP is returning values as strings which is why the === fails, but casting to int should fix that. what do you get if you echo $gettier["Privilege"];?
 
The query seems to be all goods - I ran the if statements through with a error_log(print_r($gettier,true)); at the end and it returned: "Array\n(\n [StaffID] => 1\n [Privilege] => 1\n)\n, referer: {localhost location}"
 
7:40 AM
Is that the expected privilege level for that staff member?
 
Not sure, that was all that was returned in error.log
 
You will need a WHERE clause on your query to only select the privilege for a specific staff member
 
'SELECT staff.StaffID, MAX(CAST(role.Privilege AS UNSIGNED)) AS Privilege FROM jobs INNER JOIN staff ON staff.StaffID = jobs.StaffID INNER JOIN role ON role.RoleID = jobs.RoleID WHERE staff.StaffID = 4 GROUP BY StaffID' - Upon entering this and entering the login details of this admin user, the login process still leaves a blank page
 
Have you still got the log on $gettier? What does that say?
 
8:04 AM
After
changing the query there is no inpit
input
I can send through ALL the PHP from the page if you'd like?
 
8:15 AM
What do you mean "there is no input"?
Also, you should need to GROUP BY staff.StaffID otherwise MySQL doesn't know whether you mean staff.StaffID or jobs.StaffID
 
<?php

if(isset($_POST['login-submit'])) {
require 'dbh.inc.php';
$mailuid = $_POST['mailuid'];
$password = $_POST['password'];

if (empty($mailuid) || empty($password)) {
header("Location: login.php?error=emptyfields");
exit();
}
else{
$sql = "SELECT * FROM login WHERE Username=?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: login.php?error=sqlerror");
exit();
}
else{
mysqli_stmt_bind_param($stmt, "s", $mailuid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
 
9:18 AM
You do need to change the === to ==. But you haven't said what is in $gettier after the query? you should move the error_log(print_r($gettier,true)); to immediately after the $gettier = mysqli_fetch_assoc($query); rather than having it inside the if statement
 
10:00 AM
By changing from === to == it executes NO USER FOUND header
By movin the error_log to after the gettier vairable
it prints the following
Array\n(\n [StaffID] => 4\n [Privilege] => 2\n)\n,
 
 
2 hours later…
12:16 PM
Well that is the correct result, so $gettier['Privilege'] == 2 should succeed... without access to your system to debug I can't see why that isn't going to the ADMINsuccess page
 
 
11 hours later…
11:29 PM
Looking at your code, there's no way you can execute the NO USER FOUND header if you are getting output in the error log. But you don't have an else clause on the if statement where you test the values of $gettier['Privilege'] so you should try adding one to see what output you get
 

last day (15 days later) »