-1

I'm currently trying to query based on my logged in user but i keep getting an error.

This is my homepageDemo.php

<?php
//including the database connection file
include 'config.php';

$_SESSION['userID']= $rows['userID'];

//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT m.moduleID, m.moduleName
FROM modules m
JOIN courses c ON m.courseID = c.courseID
JOIN usersDemo u ON c.courseID = u.courseID
WHERE userID = '$userID'"); // using mysqli_query instead


        //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array 
        while($res = mysqli_fetch_array($result)) {     
            echo "<tr>";
            echo "<td>"."<a>".$res['moduleID']."</a>"."</td>";
            echo "<td>"."<a>".$res['moduleName']."</a>"."</td>";
        }
        ?>

This is my accounts/login.php

<?php 
  session_start();

  include '../inc/db.php';
  include '../inc/functions.php';
  $json = [];
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = p_s($_POST['email']);
    $password = p_s($_POST['password']);


    /* ==============================================
    Data not empty
    ================================================*/
    if (!empty($email) && !empty($password)) {
      /* ==============================================
      first make sure if the email exist or not 
      ================================================*/
      if (checkIfEmailExist($email)) {

        /* ==============================================
        Check Password
        ================================================*/
        if (checkPassword($email, $password)) {

                $sql = "SELECT * FROM usersDemo WHERE email = '$email'";
                $run = mysqli_query($conn,$sql);
                while ($rows = mysqli_fetch_assoc($run)) {
                    $_SESSION['userID']= $rows['userID'];
                    $_SESSION['fName']= $rows['fName'];
                    $_SESSION['lName']= $rows['lName'];
                    $_SESSION['email']= $rows['email'];
                    $_SESSION['courseID']= $rows['courseID'];
                    $_SESSION['lastactive']= $rows['lastactive'];
                    header('Location: ../homepageDEMO.php?logged');exit();
                }

        }else{
            header('Location: ../signup.php?er1');exit();
        }
      }else{
        header('Location: ../signup.php?er2');exit();
      }
    }else{
        header('Location: ../signup.php?er3');exit();
    }


  }

These are my functions that contain my login validation

function checkIfEmailExist($email){
  global $conn;
  $data[]=array();
  $sql = "SELECT userID FROM usersDemo WHERE email ='$email'";
  $run = mysqli_query($conn, $sql);
  $rows =mysqli_num_rows($run);
  if ($rows == 0) {
    return false;
  }else{
    return true;
  }
}
function checkPassword($email, $password){
  global $conn;
  $sql = "SELECT password FROM usersDemo WHERE email ='$email'";
  $run = mysqli_query($conn, $sql);
  while ($rows = mysqli_fetch_assoc($run)) {
    $hashedDBPass = $rows['password'];
  }
  if (password_verify($password, $hashedDBPass)) {
    return true;
  }else{
    return false;
  }
}

and this is my database structure that contains the tables in question

Database Structure

Would anyone be able to assist me in where i'm going wrong?

Thanks in advance.

  • "getting an error" _What_ error? – Patrick Q Mar 08 '18 at 15:57
  • @PatrickQ Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/ubuntu/workspace/UI/homepageDEMO.php on line 13 CallStack: 0.0012 237176 1. {main()/home/ubuntu/workspace/UI/homepageDEMO.php:0 0.0021246496 2.mysqli_query()/home/ubuntu/workspace/UI/homepageDEMO.php:13 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/ubuntu/workspace/UI/homepageDEMO.php on line 17 Call Stack: 0.0012 237176 1.{main()/home/ubuntu/workspace/UI/homepageDEMO.php:0 0.0162 2466642.mysqli_fetch_array()/home/ubuntu/workspace/UI/homepageDEMO.php:17 – user9445468 Mar 08 '18 at 15:59
  • Possible duplicate of [mysql\_fetch\_array()/mysql\_fetch\_assoc()/mysql\_fetch\_row()/mysql\_num\_rows etc... expects parameter 1 to be resource](https://stackoverflow.com/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-mysql-num-rows-etc) – Patrick Q Mar 08 '18 at 16:01
  • This link you sent could help, thank you @PatrickQ – user9445468 Mar 08 '18 at 16:12
  • @PatrickQ I fixed the error, i was directing to the wrong config file. Although my query still does not work. I get back a blank page with no user data back. Do you have any idea if i could be wrong in the way i'm getting my logged in user? – user9445468 Mar 08 '18 at 16:20
  • _Which_ query doesn't work? You have many. In what way doesn't it work? When you say "a blank page", is it actually 100% blank or is there content, but just not the expected user data? Also, I don't see `session_start();` in homepageDemo.php – Patrick Q Mar 08 '18 at 16:27
  • Hi @PatrickQ , I managed to get it working based on the help you've been. Thank you – user9445468 Mar 08 '18 at 16:59
  • You're welcome. Do keep in mind the level of detail that I've prompted you for when you ask your next question. Just dumping a bunch of code and saying "It doesn't work" isn't particularly helpful for anyone (yourself included). – Patrick Q Mar 08 '18 at 17:15
  • Duly noted. Thanks @PatrickQ – user9445468 Mar 08 '18 at 17:20

1 Answers1

-1

Answer to my question above that was helped achieved within the comments, I was calling the session variable wrong.

<?php
session_start();
//including the database connection file
include 'config.php';

$cuserID= $_SESSION['userID'];

//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT m.moduleID, m.moduleName
FROM modules m
JOIN courses c ON m.courseID = c.courseID
JOIN usersDemo u ON c.courseID = u.courseID
WHERE userID = '$cuserID'"); // using mysqli_query instead



        //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array 
        while($res = mysqli_fetch_array($result)) {     
            echo "<tr>";
            echo "<td>"."<a>".$res['moduleID']."</a>"."</td>";
            echo "<td>"."<a>".$res['moduleName']."</a>"."</td>";
        }
        ?>