How to connect to database to login - PHP

Asked

Viewed 33 times

0

FORM -

<form action="" method="POST">
        <input type="text" name="username" id="username" placeholder="Username"><br><br>

        <input type="password" name="password" id="password" placeholder="Password"><br><br>

        <input type="submit" name="login" value="Login"><br><br>
    </form>

(My Connection to the database is correct, it Works) My PHP code to check the variables in the database and if they are correct to attribute them to variables

$username = $password = "";

if(isset($_POST['login'])){
    #echo " entrei no isset do login<br>";
    if (isset($_POST['username']) && $_POST['username'] != "" && isset($_POST['password']) && $_POST['password'] != "" ) {
        #echo " entrei no isset user/pass<br>";
        $username = $_POST['username'];
        $password = $_POST['password'];
        #echo $username;
        #echo $password;
        #Variaveis a funcionar e guardadas nas vars

        $sql = "Select * from conta where password = ?";
        $result = $conn->query($sql);
        if($result -> num_rows > 0){
            $row = $result -> fetch_assoc();
            echo 
            "Id:" . $row['id'] . 
            " Nome: " . $row['nome'] .
            " Username: " . $row['username'] .
            " Password: " . $row['password'] .
            " idate: " . $row['idate'] .
            " saldo: " . $row['saldo'] .
            " tipo: " . $row['tipo'];
        } else {
            echo "0 result";
        }
    } else {
        returnBack();
    }
} 

function returnBack(){

    header("Location:testar_ligabd.php");
    exit;

}

ERROR THAT I GET:

Warning: mysqli::query(): Couldn’t fetch mysqli in C: Users Leona Desktop XAMPP htdocs Projects testar_ligabd.php bets on line 73

Notice: Trying to get Property 'num_rows' of non-object in C: Users Leona Desktop XAMPP htdocs Projects testar_ligabd.php bets on line 74 0 result

  • Is missing a include with your connection file at the top of this login file. These errors occur when you cannot connect to BD.

  • @I just didn’t show it, but I got it working

No answers

Browser other questions tagged

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