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

PHP

HI,

WHats wrong with this PHP code?

<?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>

2 Answers

Just looked this one over briefly, but don't you need to have isset?

 if(isset($_POST['submit']))

update:

looks like you missed an equal-sign in your html :)

form method="post" action="index.php"
Konrad Pilch
Konrad Pilch
2,435 Points

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.

Updated my answer above.

Konrad Pilch
Konrad Pilch
2,435 Points

Lallll xd such a simple mistake : p

Thank you very much!

lol thats funny, i though u said submit. i missed a = in my submit name too xdd

My prev code was

<input type="submit" name<here>"submit" value"Log In" />

i corrected it

and i missed it even in my Log In : p

Thank you very much! I make sure i look on html next time too xd

Konrad Pilch
Konrad Pilch
2,435 Points

Could you help me with this one please?