Jump to content

[SOLVED] Check if already registered...?


sniped22

Recommended Posts

I have made a registration page and a "welcome" page. The registration page "POSTS" the data to welcome.php where welcome.php executes the following code:

 

<html>

<body>

<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "password";
$db_name = "new";
$fname = $_POST['firstname'];
$title = $_POST['title'];
$bday_m = $_POST['birthday_m'];
$bday_d = $_POST['birthday_d'];
$bday_y = $_POST['birthday_y'];
$title = $_POST['title'];
$addr = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone1 = $_POST['phone1'];
$phone2 = $_POST['phone2'];
$phone3 = $_POST['phone3'];
$prod = $_POST['prod'];
$email = $_POST['lastname'];


class db {

        function connect($db_host, $db_user, $db_pass, $db_name) {
        mysql_connect($db_host, $db_user, $db_pass);
        mysql_select_db("$db_name") or die(mysql_error());
        }

        function query($query) {
        $query = mysql_query($query) or die(mysql_error());
        return $query;
        }

        function fetch_array($query) {
        $query = mysql_fetch_array($query);
        return $query;
        }

        function fetch_row($query) {
        $query = mysql_fetch_row($query);
        return $query;
        }

        function close() {
        mysql_close();
        }
}

  $myDb=new db;

  $myDb->connect($db_host, $db_user, $db_pass, $db_name);

$myDb->query("insert into reginf(title, fullname, b_m, b_d, b_y, addr, city, state, zip, phone1, phone2, phone3, email, product) VALUES ('$title', '$fname', '$bday_m', '$bday_d', '$bday_y', '$addr', '$city', '$state', '$zip', '$phone1', '$phone2', '$phone3', '$email', '$prod')");


echo("Thank you for registering, ");
echo($_POST['firstname']);

  $myDb->close();
?>

</body>
</html>

 

but I want to use an if statement to check if the email has already been associated with another account. Something like this:

 

if($a['email'] already exists)

{

echo("the email you have entered is registered to another account");

}

else

{

$myDb->query("insert into reginf(title, fullname, b_m, b_d, b_y, addr, city, state, zip, phone1, phone2, phone3, email, product) VALUES ('$title', '$fname', '$bday_m', '$bday_d', '$bday_y', '$addr', '$city', '$state', '$zip', '$phone1', '$phone2', '$phone3', '$email', '$prod')");

}

 

so that it only inserts the data if email doesnt already exist in the database.

please guide me along with this if statement to check the database for the email, and if it exists or doesnt exist execute the respective code.

 

Link to comment
Share on other sites

Learn how to make a column unique within the database itself.

 

CREATE TABLE users(
...
Email VARCHAR(255) NOT NULL DEFAULT '',
...
UNIQUE(Email)
)

 

Now when you try and insert a duplicate user with the same e-mail address, the database will refuse to do so.  So in your script, if the INSERT statement fails you know the user wasn't registered.

 

Note that a VARCHAR(255) is not necessarily sufficient for all possible e-mails, I just used that column for simplicity here.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.