-1

when i log on to my user account it shows these warning:-

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /hermes/bosnaweb24a/b1323/ipg.techbitercom1/duracomexteriors/admin/action/action_login.php on line 12 Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosnaweb24a/b1323/ipg.techbitercom1/duracomexteriors/admin/action/action_login.php:12) in /hermes/bosnaweb24a/b1323/ipg.techbitercom1/duracomexteriors/admin/action/action_login.php on line

i'm attaching my action_login.php code

<?php

    require_once("../../db_connection/dbConnection.php");
    session_start();
    if(isset($_POST["ok"])){
        $username=$_POST["username"];
        $password =$_POST["password"];

            if(mysqli_query($con, "select * from tbl_admin_log where user_name='$username' or email='$username' and password='$password'")){
                $_SESSION["login"] = $username;
                $_SESSION["passw"] = $password;
                while($ln = mysqli_fetch_array($log, MYSQLI_ASSOC)){
                    $_SESSION["log_type"] = $ln["type"];
                }
                header("location:../admin.php");
            }
            else{
                header("location:../index.php?failed=Invalid Username or Password!");
            } 
    }

    ?>
Ali
  • 3,373
  • 5
  • 42
  • 54
abhi
  • 1
  • session_start always need to be very first line of the script. – Gyan Jul 12 '17 at 04:51
  • 2
    Possible duplicate of [mysqli\_fetch\_array()/mysqli\_fetch\_assoc()/mysqli\_fetch\_row() expects parameter 1 to be resource or mysqli\_result, boolean given](https://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1) – JYoThI Jul 12 '17 at 04:54
  • 1
    $log is not defined anywhere, – sumit Jul 12 '17 at 04:58
  • again showing the same results – abhi Jul 12 '17 at 05:03
  • You need to define the result from the query with `if ($log = mysqli_query(.....)) {` - then you should group the conditions in the query, or you will experience undesired results. Lastly, you should use prepared statements with placeholders in the query to prevent sql injection. – Qirel Jul 12 '17 at 05:17
  • this worked for me, thanks for your valuable reference – abhi Jul 12 '17 at 15:50

3 Answers3

-1

Put session_start() above your require_once() statement.

Difster
  • 3,264
  • 2
  • 22
  • 32
-1

Use something like this and see if it works

session_start();    
require_once("../../db_connection/dbConnection.php");

if(isset($_POST["ok"])){
  $username=$_POST["username"];
  $password =$_POST["password"];

  $checkLogin = mysqli_query($con,"SELECT * FROM tbl_admin_log WHERE (user_name='$username' OR email='$username') AND password='$password'") or die(mysqli_error($con));      
  if(mysqli_num_rows($checkLogin)>0){
    $_SESSION["login"] = $username;
    $_SESSION["passw"] = $password;
    $checkLoginRow = mysqli_fetch_assoc($checkLogin);
    $_SESSION["log_type"] = $checkLoginRow["type"];
    header("location:../admin.php");
  }else{
    header("location:../index.php?failed=Invalid Username or Password!");
  } 
}
Gyan
  • 498
  • 6
  • 10
  • One question abhi, do you have username in both username and email columns in database table? – Gyan Jul 12 '17 at 05:37
-1

1st : Move your session_start() on very top of page

2nd : you need to group the or condition using paranthesis like this (user_name='$username' or email='$username')

3rd : First check the query is success or not then count the result using this if condition if(($log) && mysqli_num_rows($log)>0){ }

"select * from tbl_admin_log where (user_name='$username' or email='$username') and password='$password'"

PHP :

$log = mysqli_query($con, "select * from tbl_admin_log where (user_name='$username' or email='$username')  and password='$password'") or die(mysqli_error($con));

 if(($log) && mysqli_num_rows($log)>0){

        $_SESSION["login"] = $username;
        $_SESSION["passw"] = $password;
        $ln = mysqli_fetch_assoc($log, MYSQLI_ASSOC);
        $_SESSION["log_type"] = $ln["type"];

        header("location:../admin.php");
}
else{

    header("location:../index.php?failed=Invalid Username or Password!");
} 
JYoThI
  • 11,977
  • 1
  • 11
  • 26