mysql - Asking With Login PHP Mysqli

325

I am new to this PHP login script stuff but I do want to learn.

I have dbconect.php like this:

<?php
//core
function dbcon(){
    $user = "root";
    $pass = "";
    $host = "localhost";
    $db = "test";
    $connect = mysqli_connect($host,$user,$pass,$db);
    $select = mysqli_select_db($connect,$db);
}

function host(){
    $h = "http://".$_SERVER['HTTP_HOST']."/test/";
    return $h;
}

function hRoot(){
    $url = $_SERVER['DOCUMENT_ROOT']."/test/";
    return $url;
}

//parse string
function gstr(){
    $qstr = $_SERVER['QUERY_STRING'];
    parse_str($qstr,$dstr);
    return $dstr;
}

?>

And i do have login.php like this:

 <?php
    include('admin/lib/dbcon.php');
    dbcon(); 
    session_start();    
    $username = $_POST['username'];
    $password = $_POST['password'];

    /*................................................ admin .....................................................*/
        $query = "SELECT * FROM admin WHERE username='$username' AND password='$password'";
        $result = mysqli_query($connect,$query)or die(mysqli_error($connect));
        $row = mysqli_fetch_array($result);
        $num_row = mysqli_num_rows($result);

    /*...................................................Technical Staff ..............................................*/
    $query_client = "SELECT * FROM client WHERE username='$username' AND password='$password'";
    $result_client = mysqli_query($connect, "SELECT * FROM client WHERE username='$username' AND password='$password'")or die(mysqli_error($connect));
    $num_row_client = mysqli_num_rows($query_client);
    $row_client = mysqli_fetch_array($query_client);

    if( $num_row > 0 ) { 
    $_SESSION['id']=$row['admin_id'];
    echo 'true_admin';

    mysqli_query($connect, "insert into user_log (username,login_date,admin_id)values('$username',NOW(),".$row['admin_id'].")")or die(mysqli_error($connect));

    }else if ($num_row_client > 0){
    $_SESSION['client']=$row_client['client_id'];
    echo 'true';

    mysqli_query($connect, "insert into user_log (username,login_date,client_id)values('$username',NOW(),".$row_client['client_id'].")")or die(mysqli_error($connect));

     }else{ 
            echo 'false';
    }    

    ?> 

I also had made the table SQL

The question is: What is wrong so i can not login using those code?

very appreciate with all the answer

thank you very much

348

Answer

Solution:

/...................................................Try This ............................................../

$query_client = "SELECT * FROM client WHERE username='$username' AND password='$password'";
$result_client = mysqli_query($connect, $query_client);   //pass your method here rather than duplicate query
$num_row_client = mysqli_num_rows($query_client);
$row_client = mysqli_fetch_array($query_client);

if( $num_row > 0 ) { 
$_SESSION['id']=$row['admin_id'];
echo 'true_admin';

mysqli_query($connect, "insert into user_log (username,login_date,admin_id)values('$username',NOW(),".$row['admin_id'].")")or die(mysqli_error($connect));

}else if ($num_row_client > 0){
$_SESSION['client']=$row_client['client_id'];
echo 'true';

mysqli_query($connect, "insert into user_log (username,login_date,client_id)values('$username',NOW(),".$row_client['client_id'].")")or die(mysqli_error($connect));

 }else{ 
        echo 'false';
}    

People are also looking for solutions to the problem: php - Create H:i value in Carbon from gmdate()

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.