Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Konrad Pilch
Konrad Pilch
2,435 Points

[SOLVED] PHP Log In

HI,

I was coding it to the mysqli database called learnlearn into members . It seem it doesnt work, not even the echo if i press it.

<?php

    error_reporting(E_ALL & ~E_NOTICE);
    session_start();

    if($_POST['submit']) {
        include_once("connection.php");
        $username = strip_tags($_POST['username']);
        $password = strip_tags($_POST['password']);

        $sql = "SELECT id, username, password FROM members WHERE username = 'username'AND activated = '1' LIMIT 1";
        $query = mysqli_query($dbCon, $sql);

        if($query) {
            $row = mysqli_fetch_row($query);
            $userId = $row[0];
            $dbUsername = $row[1];
            $dbPassword = $row[2];
        }

        if ($username == $dbUsername && $password == $dbPassword) {
            $_SESSION['username'] = $username;
            $_SESSION['id'] = $userId;
            header('Location: user.php');
        } else {
            echo "Incorrect username or password.";
        }


    }

?>

<!DOCTYPE>
<html>
<head>
    <title>PHP/MySQL Login</title>
</head>
<body>

    <h1>PHP/MySQL Login</h1>

    <form method="post" action"index.php">
        <input type="text" placeholder="Username" name="username" /> <br />
        <input type="password" placeholder="Password" name="password" /> <br />
        <input type="submit" name"submit" value"Log In" />
    </form>

</body>
</html>

Whats wrong with it?

It is connected to the database and i re-done it twice. Same effects.

What does the log file say? What error messages are you getting? Without this information no one can help you.

Konrad Pilch
Konrad Pilch
2,435 Points

Nothing. Its just blank.

If the page is blank then the error would exist in your log file. What does the log file say?

Konrad Pilch
Konrad Pilch
2,435 Points

Thank you but The page wasnt balank xd. I forogt to put the equal sign in the submit and the values in the html. : p

But now i have another problem which i put here.

2 Answers

Try putting an 'exit' after header('Location: user.php');

Konrad Pilch
Konrad Pilch
2,435 Points

I did thath too.

I tried even this :

<?php

    error_reporting(E_ALL & ~E_NOTICE);
    session_start();

     if(isset($_POST['submit'])) {
        echo 'It doesnt work, why?';
    }

?>

<!DOCTYPE>
<html>
<head>
    <title>PHP/MySQL Login</title>
</head>
<body>

    <h1>PHP/MySQL Login</h1>

    <form method="post" action"index.php">
        <input type="text" placeholder="Username" name="username" /> <br />
        <input type="password" placeholder="Password" name="password" /> <br />
        <input type="submit" name"submit" value"Log In" />
    </form>

</body>
</html>

It doesn't work.

Konrad Pilch
Konrad Pilch
2,435 Points

This challenge is solved. Thank you for trying to help.