-3

I want to check if a user exists through my address bar using the GET method. if that users exist then echo $username and $firstname. but It display 2 errors. I don't know what's wrong!.

 <?php include ("./trial/header.php"); ?>
<?php
    //we want to perform  http://localhost/test.php?=jude to see if user exists then displays username and first name
    //the table name is users and include username, first_name, last_name, password and email.
if (isset($_GET['u'])) {
    $username = mysqli_real_escape_string($con, $_GET['u']);
    if (ctype_alnum($username)) {
    //check if user exists
    $check = mysqli_query($con, "SELECT username, first_name FROM users WHERE username='$username'");
    //if user exists we want to display username and firstname on the page
    if (mysqli_num_rows($check)===1) {
    $get = mysqli_fetch_assoc($check);
    $username = $get['username'];
    $firstname = $get['firstname']; 
    }
    //if user do not exist we want to to display "The user does not exist"
    else
    {
    echo "The user does not exist";  //no existing users
    exit();
    }
    }
}
?>

    <h2>Profile page for: <?php echo $username;?></h2>;
    <h2>First name: <?php echo $firstname;?></h2>;
s.purpose
  • 45
  • 1
  • 2
  • 9

1 Answers1

0

You have problem in Your if statement Your code is not entering in it... As a workaround i can give you code...

<?php include ("./trial/header.php"); ?>
<?php
    //we want to perform  http://localhost/test.php?=jude to see if user exists then displays username and first name
    //the table name is users and include username, first_name, last_name, password and email.
$username = ""; //Added this 
$firstname = "";//Added this
if (isset($_GET['u'])) 
{
    $username = mysqli_real_escape_string($con, $_GET['u']);
    if (ctype_alnum($username)) 
    {
        //check if user exists
        $check = mysqli_query($con, "SELECT username, first_name FROM users WHERE username='$username'");
        //if user exists we want to display username and firstname on the page
        if (mysqli_num_rows($check)===1) 
        {
            $get = mysqli_fetch_assoc($check);
            $username = $get['username'];
            $firstname = $get['first_name']; 
        }
        //if user do not exist we want to to display "The user does not exist"
        else
        {
            echo "The user does not exist";  //no existing users
            exit();
        }
    }
}
?>

<h2>Profile page for: <?php echo $username;?></h2>;
<h2>First name: <?php echo $firstname;?></h2>;

For a proper output your should use this url http://localhost/test.php?u=jude in place of http://localhost/test.php?=jude As Md. Sahadat Hossain Suggested.

Praveen Kumar
  • 2,408
  • 1
  • 12
  • 20