Jump to content

GregRickshaw

Members
  • Posts

    10
  • Joined

  • Last visited

GregRickshaw's Achievements

Member

Member (2/5)

1

Reputation

  1. The login code just does that I just built it into a function to call exactly as you said. function current_user() { if (is_user_logged_in()) { return $_SESSION['email']; } return null; } I went this route because I just couldn't work it out how to use the key. Ironically of course my noddy testing and coding took way longer than probably revisiting that properly. The db function is this? Is this not good way of doing things? function db(): PDO { static $pdo; if (!$pdo) { $pdo = new PDO( sprintf("sqlsrv:server=%s;Database=%s",DB_HOST, DB_NAME), DB_USER, DB_PASSWORD, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] ); } return $pdo; } Thanks for all your help. Always learning, always looking for better ways. I'm a MSSQL/BI developer/data architect by day, so all of this is new to me
  2. I will do thank you for the guidance. Thank you yes it's a bit messy I have a lot to learn particularly etiquette Will do, I'm trying my best to bumble along, I'm not looking to become a PHP developer when I get the POC complete I will be handing it over to the pros. Thanks again - take care
  3. Brilliant I got it now works a treat, onwards and upwards Thank you again for your patience, time and effort. <?php $User = current_user(); $sql = 'SELECT fk_usertypes_id FROM users WHERE email=:User'; $statement = db()->prepare($sql); $parms = array('User'=>$User); if (!$statement->execute($parms)) { echo "Query did not run"; exit(); } while($row = $statement->fetch(PDO::FETCH_ASSOC)) { echo "UserType " . $row['fk_usertypes_id'] . '</br>'; if ($row['fk_usertypes_id'] ==1) { echo "Hello 1";} else { echo "Hello 2"; } } ?> <p><a href="index.php">Home</a></p> <?php view('footer') ?>
  4. Ok that works very nicely. I don't need to echo the value out really, that was just for me to see what was going on. It works each time. Yes current_user() is a function and gets the email from the login page to know who I am dealing with. Yes the $bob makes now sense I've removed it now, again it was just my way of finding out what was going on. Which is why I was trying to test the value in the first place How would I then achieve a test of the value? Rather than showing it on screeen. So now I have the UserType being set correctly I wanted to do something like if UserType = 1 then do this (go to a page for instance) if UserType = 2 then do this (go to a different page for example) Many thanks for your time, effort and help.
  5. Making no sense is correct, I am just dabbling around I'm not trying to do or be anything so apologies as I just don't really know I'm just finding my way. I have tried testing it as you say but it always the same. I have tried not used the fetchAll() it's always the same I only looped over the data because I was testing for more than one value but I can remove that. I will try the things you mention and come back to you. I have also tried in_array function still always ignores it.
  6. print_r($bob); if ($bob==1) {echo "Hello 1";} else { echo "Hello 2"; } Made the change suggested, which did work when the user was type 2 but when the user is type 1 the output is always the else statement Hello 2 Thanks I will add those to my code will come in handy with errors. Great tip
  7. Thanks again Barand. I was sure had tried that I actually had === in there for a while as I read that too. Anyway just doing == has fixed the problem however when the user is 1 it now goes automatically to 2 Anything to say if you are 1 echo 1 if you are 2 echo 2
  8. Hi all I have value passed into an array using a SQL query which appears to work fine. The value I want is passed into $usertype However I then want to test the $usertype for the value in the array. I know the value is in position [0] in the array but specifying this seems to make no difference anyway The output from the code below is the screenshot It recognises I am User Type 2 ($bob) However if I test $bob which according to the array is 2 and correctly output as 2. The test always thinks bob is 1 Anyone got any help on how to test a value in an array? Code is below <?php $User = current_user(); $sql = 'SELECT fk_usertypes_id FROM users WHERE email=:User'; $statement = db()->prepare($sql); $statement->bindValue(':User', $User, PDO::PARAM_STR); $statement->execute(); $usertype = $statement->fetchAll(PDO::FETCH_ASSOC); if ($usertype) { foreach ($usertype as $bob) { echo '<left>' . "UserType " . $bob['fk_usertypes_id'] . '</br>'; } } print_r($bob); if ($bob=1) {echo "Hello 1";} else { echo "Hello 2"; } ?> <p><a href="index.php">Home</a></p> <?php view('footer') ?>
  9. WOW works a treat thank you so much I don't think I would ever have done that. A lot to learn but it's every exciting and enjoyable.
  10. Hi All Absolute newbie here, been using PHP for about three months, yes this is a homework project, yes there are a ton of mistakes, yes I may be open to SQL Injection but this is a total homework project on my laptop and destined never to go anywhere. I have written two custom functions to check a login via a html form. All works well but now I have decided I want to check what type of user is logging in to direct the users to a different page, eg: Standard user goes to one page Admin user to a different. So this code works just fine: Finds the users email and logs the user in with the correct email so I get a different user for each email. However, what I want to do now is pass the type of user FK_UserTypes_Id which will be a 1, 2 or 3 etc., I thought I would just be able to add a variable of usertype and the SQL would pull that value into my new variable I can then use this further down the coding However as soon as I introduced another parameter I get this error message: Fatal error: Uncaught ArgumentCountError: Too few arguments to function find_user_by_username(), 1 passed in C:\laragon\www\auth\src\auth.php on line 160 and exactly 2 expected in C:\laragon\www\auth\src\auth.php:1 I have run the SQL many times and the fk_User_Id does return for the email provided. I'm learning so I'll continue to delve and try stuff but this has had me more stumped than any problem in PHP I have encountered so far. Many thanks for looking G
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.