sniped22 Posted February 15, 2007 Share Posted February 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted February 15, 2007 Share Posted February 15, 2007 Do something like SELECT * FROM reginf WHERE email='{$email}' and then if(mysql_num_rows($result_here) > 0) { echo "email exists"; } Also, it isn't necessary to redeclare all the variables. Just access them directly Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 15, 2007 Share Posted February 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
sniped22 Posted February 15, 2007 Author Share Posted February 15, 2007 CREATE TABLE users( ... Email VARCHAR(255) NOT NULL DEFAULT '', ... UNIQUE(Email) ) what would this do? Would it just create a table where column "email" is not null by default, setting the name to unique instead of null? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 16, 2007 Share Posted February 16, 2007 It makes the Email column in the table unique, which means you can't have two rows in the table with the same Email address. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.