Jump to content

Need Help php code to look up username based on email address.


funkymonkey

Recommended Posts

Hi all,

 

I'm an accounting student working on an IT manager bachelors degree.

 

The course I am working on is Database management for Financial Managers.

 

The question I am working on deals with php code, but in the text they never really talk about php but they give some example code used to accept user input to register for a course. The code they show is divided into 3 parts (1) PHP script initiation and input validation (starts with HTML code to setup generic page, checks wether to process form, and parts to validate all require fields) (2) Adds user info to database and (3)Closes php script and displays html form.

 

Using this example I am suppose to write a script that accepts a e-mail address as the input, queries the database with the e-mail address value, and returns for display the associated username.

 

I am totally lost.

 

Can anyone provide some help?

I suggest you read over the examples again. Short of us doing it for you, I can't see why our examples would be any clearer.

 

The process is simple. A form posts an email address to a php script. In your script you will find the email address within the $_POST array. Put it into an sql statement (string). You then execute that sql statement via mysql_query which returns a result resource containing the user name your after.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="email">
<input type="submit" value="Search">
</form>

<?php

if(isset($_POST['email']) && strlen($_POST['email'])>0){
    $email=mysql_real_escape_string($_POST['email']);
    $check=mysql_query("SELECT username FROM tablename WHERE email='$email'");
    if(mysql_num_rows($check)==1){
        $email=mysql_fetch_array($check);
        echo 'Email: '.$email[0];
    }

    else{
        echo 'No results found!';
    }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.