0

I want to check if the user exists in the database when clicking 'log in'. If so, echo "user exists" and if not "user does not exist". How can i go about this? so far i have:

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

?>

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>

<body>
<form method="post" action="login.php">
Username: <input type="text"  value="" name="username" /><br /><br />
Password: <input type="password" value="" name="password" /><br /><br />
<input type="submit" name="submit" value="Log In"/>
</form>
</body>
</html>
1
  • Hint: SELECT COUNT(*) ... Commented Dec 3, 2014 at 21:18

2 Answers 2

2

Try this one

session_start();
if($_SERVER['REQUEST_METHOD'] === 'POST'){

    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "test";

// Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $user = $conn->real_escape_string(htmlspecialchars(trim($_POST['username'])));
    $query = "SELECT `username` FROM `users` WHERE `username` = '$user'";

    $result = $conn->query($query);
    if($result->num_rows > 0) {
       $_SESSION['allowToWelcome'] = true;
       header('Location:welcome.php');
       die();
    }
    else $message = 'user does not exist';

}

?>

    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Test</title>
        </head>

        <body>
            <form method="post" action="login.php" enctype="multipart/form-data">
                <?php if(isset($message)) : ?>
                    <div class="error"><?php echo $message; ?></div>
                <?php endif; ?>

                Username: <input type="text"  value="" name="username" /><br /><br />
                Password: <input type="password" value="" name="password" /><br /><br />
                <input type="submit" name="submit" value="Log In"/>
            </form>
        </body>
    </html>

in welcome.php you can check like this

session_start();
if(!isset($_SESSION['allowToWelcome'])){
   header('Location:login.php');
   die();
}
Sign up to request clarification or add additional context in comments.

4 Comments

I want to take the user to a 'welcome page' if they exist, if they do not, display 'user does not exist'. How do i link to another page? do i need to do this in the html, form action?
Modified code above. Check line header('Location:welcome.php'); You can change welcome.php to any page in your project.
thanks again! for some reason, this still lets in-existing users to access the welcome.php for example..
Modified again, last time. I think I've gave you full answer
1

On my login/register script I made, I used something along the lines of this..

// check existing username
$prep_stmt = "SELECT id FROM your-table-name WHERE username = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);

if ($stmt) {
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $stmt->store_result();

            if ($stmt->num_rows == 1) {
                    // A user with this username already exists
                    $error_msg .= '<p class="error">A user with this username already exists</p>';
                    $stmt->close();
            }
            $stmt->close();
    } else {
            $error_msg .= '<p class="error">Database error line 55</p>';
            $stmt->close();
    }

3 Comments

Should add the mysqli prepare function and execute :)
@DarkBee it might be a homework for OP actually :-) Even a query is already too much for a newbie
Sorry, I am just learning PHP / MySQL, and only know so much, figured I'd try to help someone out. Added my full function and if/else conditional statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.