Problem with Login

Asked

Viewed 78 times

0

I’m trying to create a basic login, with verification so that there are no records with even users in the database. But I’m making a mistake on this line $row = $resulta->fetch_assoc();

Fatal error: Uncaught Error: Call to a Member Function fetch_assoc() on Boolean in...

    $result = "SELECT * FROM usuario WHERE usuario = '$usuario' AND senha = '$senha' ";
    $resulta = $conn->query($result);
    $row = $resulta->fetch_assoc();

    if ($resulta->num_rows > 0) {  
        echo "<script>alert('Usuário Já Existente');</script>"; 
    } else {    
        $result = "INSERT INTO Cadastro (usuario, senha, nivel_acesso) VALUES ('$usuario', '$senha', '$nivel_acesso')";
        $resultado = mysqli_query($conn, $result);      
    }

Does anyone know how to fix this so that there is user verification before entering into the bank ?

  • 2

    The $query is invalid

  • 1

    wouldn’t be: $result->mysqli_fetch_assoc() ?

  • @Anthraxisbr also did so, but no way works. I wanted to check if there is already a registered user name in the bank, if yes alert user and otherwise enter in the bank... but is giving error

  • 1

    @Anthraxisbr, no. The guy is using the object-oriented method of mysqli. https://secure.php.net/manual/en/mysqli-result.fetch-assoc.php

  • 2

    $results is false @Anthraxisbr

  • @Sveen would have some other logical way to make it work ?

  • 1

    if ($result->num_rows > 0) ? while($Row = $result->fetch_assoc()) ? ..... try doing a while while

  • 1

    Table name and fields are correct? Remember that it is case sensitive

  • 1

    @Arsomnolasco $result is false, not a valid object

  • 1

    try with SELECT u.* FROM user u WHERE u.user LIKE '$user' AND u.password LIKE '$password'

  • 1

    usuario='{$usuario}' so try in password tbm

  • 1

    I think Mysql can confuse the name of the field "user" with the table "user"

  • 1

    The code posted works perfectly well. I tested it on my server. The problem may be in the part of the code not posted.

  • 1

    @Victor tests my answer and give me a return

  • 2

    It is necessary to post complete code, connection and table structure so that someone can identify the problem because as I commented before, the code posted works perfectly well on my server.

  • 1

    Just so we’re clear, the code you have is susceptible to attacks from mysql Injection. Something to consider later after solving the problem.

  • 1

    @Leocaracciolo is right. The problem is probably related to the other part of the code.

  • Thanks to everyone who helped, I developed the code quickly and was moving from the old PHP to PHP 7, I ended up getting so caught up that I created a.select condition for a table other than the one I wanted for my Insert. The error is in the select of the same table, should be select * from Register

Show 13 more comments

2 answers

1


The problem is related to SQL.

In my view there is an inconsistency in your query. It called me the attention you check in the table usuário if there is a record and registered in the table Cadastro this record that does not exist, without adding a record in the user table.

In addition the table usuario has the same name as her field, and the same table fields Cadastro. Indicating that there was probably some confusion in the nomenclatures.

Therefore, I believe the error is in the table name of your first query. This table usuario probably doesn’t exist:

$result = "SELECT * FROM usuario WHERE usuario = '$usuario' AND senha = '$senha' ";

The right thing would be Cadastro:

$result = "SELECT * FROM Cadastro WHERE usuario = '$usuario' AND senha = '$senha' ";

I know this because I took the test without the user table and error was the same:

Fatal error: Uncaught Error: Call to a Member Function fetch_assoc() on Boolean in

  • 1

    The error can also happen if the field name is not incorrect, or any syntax error

  • 1

    Yes @Sveen, however, I analyzed the incongruity of the information. In the first table its name is the same as the name of the field, ie user. The registration table seems to me more coherent because the two tables have the same fields written equally. Anyway was edit the answer to make it more generic. Thanks for the comment.

  • 1

    @See what you think =)

  • 1

    Thank you, I was so worried about moving from old PHP to PHP 7 that I only attach myself to fetch, mysqli, the hahaha querys and I didn’t even see that I called the different table. And believe me I was as correct as the name of the table I spent 1 hour checking everything in the code except those lines I posted, I couldn’t find the solution and I decided to post here to see if anyone thought the error, I knew I was in these lines but I didn’t realize thanks kkkk

  • @Victor kkkk happens. Hug!

0

Change the login form, do so:

$query = "SELECT * FROM `Cadastro` WHERE usuario = '$usuario' AND senha = '$senha' ";

if($con->query($query)->fetch_all()){
    //Usuário existe
    echo 'Existe';
 }else{
    //Usuário não existe
     $result = "INSERT INTO Cadastro (usuario, senha, nivel_acesso) VALUES ('$usuario', '$senha', '$nivel_acesso')";
        $resultado = mysqli_query($conn, $result);  
 }
  • But $con->query($query) returns false. read the question

  • I find it unnecessary to make a fetch_all in this case since it is only necessary to check if 1 record is found

  • @Phiter fetch_all only brings the found, if nothing found, it already returns false, making it not need to count the number of dps lines

  • Yeah, considering that the user and password are unique, it makes sense. I don’t know which is the fastest, fetch_all or row_count.

  • @Sveen how false returns? Only if there is no db record or sql is wrong, otherwise no return

  • @Phiter if there is a difference in speed, I think it should be inconspicuous, in which case it still saves lines in code

  • http://php.net/manual/en/mysqli.query.php. Returns FALSE on Failure. For Successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will Return a mysqli_result Object. For other Successful queries mysqli_query() will Return TRUE.

  • @Sveen the failure is only in sql, even if there is no record it still returns true by completing the query

  • So his problem is to solve the problem in the query, and not to leave faster saving line, understood?

  • @Sveen but the query has no error, unless the variables are not receiving the proper value

  • Fatal error: Uncaught Error: Call to a Member Function fetch_assoc() on Boolean in.... How to run a ->fetch_all() on a boolean?

  • @Sveen yes my friend, what has this got to do with it? Did you at least test what I put here? I’m giving him a different solution than he wants

  • I wasn’t the one who said no

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.