...

View Full Version : Code to say that the email is in use, or code to say that certain fields are missing



Trusten
07-28-2002, 10:34 AM
just as the title says.

ok, in the script i have, i didn't get the 'your email address is already in use' so how do i write that.

there are two parts, one for userid in use, and one for passwords don't match

see, like so



if($signuppassword!=$signuppasswordverify) {
create_error_page_passwordsnotmatch();
or

if ($num_rows == 1) {
create_error_page_useridinuse();

what do i do now? to make it look for a unuque email

i want to learn how to edit a script myself.

Phip
07-28-2002, 11:39 AM
<?php

// searches the table for that email and selects it if it finds it
$getQuery = "SELECT * FROM table WHERE email='$email'";

$result = mysql_query($getQuery);

// mysql_numrows() gets the number of rows selected
// if it finds the email address it will return 1 (used)
$checkE = mysql_numrows($result);

// return an error
if($checkE >= "1")
{
echo "Invalid E-mail address, ". $email ." is already used";

// halt and exit script so nothing else will be exicuted
exit;
}


?>

Phip
07-28-2002, 11:45 AM
for checking for blank fields..




<?php

if($formField1 == "")
{
echo "you left formField 1 blank";
exit;
}
if($formField2 == "")
{
echo "you left formField 2 blank";
exit;
}
if($formField3 == "")
{
echo "you left formField 3 blank";
exit;
}

?>


i would recommend using javascript for this though. it is much faster and also keeps the user from having to go back and filling out the form over agian. which would be a real pain on a big form.


<script language="javascript">
function check()
{

if(document.FORMNAME.FIELDNAME.value == "")
{
// alert the user of blank field
alert("FIELDNAME was left blank!";

// return false to stop submition
return false;
}

}
</script>

use the onsubmit to check:

<form method="" action="" onsubmit="return check()">

Trusten
07-28-2002, 11:46 AM
i tried adding the email code into the script, it didn't work.


should i post the script for you to show me where to put that string, or should i just include it at teh top as if it's a seperate script?

Phip
07-28-2002, 11:48 AM
yeah go ahead and post the whole thing

go here for an example of the javascript. just press post

http://phip.chronicyouth.com/chronicbook/index.php?do=sign-book

Trusten
07-28-2002, 12:06 PM
<?
// User Sign Up PHP Script
//
// This script validates information, saves it to the database
// displays it to them
// and sends it to them via email
//
// vars:
// $signupusername
// $signuppassword
// $signuppasswordverify
// $signupemailaddress
//
// Note have to use signup* vars initially to make it clear to ourselves that
// we're dealing with signupotherwise

// Include common variables and message text
include 'zcommon.php';

//null out cookies if new user
//null out cookies at start of sign in routine
//all cookies begin with "ck_" to indicate that they are a cookie
//helps troubleshoot mysterious cookie errors
// note on using cookies.
// MUST BE SET before ANY http output.
// They TRAVEL in the http HEADER so have to go first.
setcookie ("ck_user", "");
setcookie ("ck_password", "");
setcookie ("ck_user_id", "");

// 0th check that passwords match
if($signuppassword!=$signuppasswordverify) {
create_error_page_passwordsnotmatch();
}
else {
// their passwords match so enter next validation stage
// first test that their username isn't already in use

// Connecting, selecting database
$link = mysql_connect("$dbhost", "$dbuser", "$dbpassword")
or die("Could not connect");
//select the database
mysql_select_db("$db")
or die("Could not select database");
//try and select the username that the user entered to see if it is already in the db
$query = "SELECT userid "
. " FROM users "
. " WHERE userid='$signupusername'";

$result = mysql_query($query)
or die("Query failed at username unique testing stage.");

//logic -- if the num_rows is 1 then the username is already in use
// and they have to choose another
$num_rows = mysql_num_rows($result);
//don't need to get it -- its the same as what we already have

//num_rows can't ever be >= to 1 since unique constraint on the column of data
if ($num_rows == 1) {
create_error_page_useridinuse();
}
else {
// NOTE -- Depending on how you want to define
// a valid password (5 chars, 6 chars
// plus a number, etc), that would go here

//Capture the ipaddress and call MD5
$ipaddress = getenv ("REMOTE_ADDR");
$encryptedpassword = md5($signuppassword);

//try and add them to the database
$query = "INSERT INTO users "
. " ( userid, password, date, ipaddress, email, fullname, hintquestion, hintanswer ) "
. " VALUES ('$signupusername','$encryptedpassword',
NOW(), '$ipaddress', '$emailaddress', '$signupname', '$signuphint', '$signupanswer')";

//execute the query
$result = mysql_query($query)
or die("Query failed at user insertion stage.");

//now query the db back for the user_id variable
$query = "SELECT user_id "
. " FROM users "
. " WHERE userid='$signupusername'";

// get the result
$result = mysql_query($query)
or die("Query failed at userid retrieval stage.");

//get the user_id from the result
$num_rows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
$user_id = $row[0];

// send the cookies now. MUST BE FIRST THING OUTPUT
setcookie ("ck_userid", "$signupusername");
setcookie ("ck_password", "$signuppassword");
setcookie ("ck_user_id", "$user_id");

//Define the $title variable for the page title
$title = "Thanks for Signing Up!";

//Set up the page header
PrintPageHeader("$title");

//Print out the body of the page
// Note that some basic html formatting is used
// here to make it look better

print "<TABLE WIDTH=728 BGCOLOR=WHITE><TR><TD>";
print "<H1>Thank You...</H1>";
print "<HR>";
print "<CENTER>Welcome $signupname! ";
print "Hope you enjoy the ";
print "site.</CENTER><BR><BR>";
print "Here is the information that you entered:";
print "<UL>";
print "<LI>Username: $signupusername</LI>";
print "<LI>Password: $signuppassword</LI>";
print "<LI>Email Address: $emailaddress</LI>";
print "</UL>";
print "<BR>We have also emailed this to the email ";
print "address you gave us.<BR>";
print "</TD>";
print "</TR>";
print "<TABLE>";

//Usually you want to add a link to your
// application's home page here.
// Left as an exercise for the reader.

//handle sending out the email if we got an email address!
if ($emailaddress != "") {
// compose the email
$to = $emailaddress;
$subject = "Your Membership";
$message = "Welcome $signupname, "
. "

Your account has been created and is ready for use."
. ""

. ""
. "

Your username is: $signupusername"
. "
Your password is: $signuppassword"
. ""
. "

Thanks for signing up to A."
. "

http://www.me.com/"
. ""
. "

Jewle,
Webmaster
webmaster@me.com";
// send the email
mail($to, $subject, $message, "From: webmaster@me.com", "-
fwebmaster@me.com");
}

//Set up the footer of the page
PrintPageFooter("");
}
}

// start of functions

function PrintPageHeader ($title) {
print "<HTML>";
print "<HEAD>";
print "<TITLE>";
print "$title";
print "</TITLE>";
print "</HEAD>";
print "<BODY>";
}

function PrintPageFooter ($title) {
print "</BODY>";
print "</HTML>";
}


function create_error_page_passwordsnotmatch() {
//Define the $title variable for the page title
$title = "We're Sorry But You're Passwords Don't Match!";

//Set up the page header
PrintPageHeader("$title");

print "<TABLE WIDTH=728 BGCOLOR=WHITE><TR><TD>";
print "<H1>We're Sorry...</H1>";
print "<HR>";
print "<BR>";
print "We're sorry but You didn't enter matching passwords. ";
print "&nbsp;Please make sure that you enter your password ";
print "in the Password field and the Verify Password fields ";
print "and that both are the same.<BR><BR>";
print "Please press the back button and make sure that the ";
print "passwords match.";

// close container table
print "</TD></TR></TABLE>";

//Set up the footer of the page
PrintPageFooter("");
}


function create_error_page_useridinuse() {
//Define the $title variable for the page title
$title = "We're Sorry But that Username is Already in Use...";

//Set up the page header
PrintPageHeader("$title");

print "<TABLE WIDTH=728 BGCOLOR=WHITE><TR><TD>";

print "<H1>We're Sorry...</H1>";
print "<HR>";
print "<BR>You entered a Username that another person is using";
print "<BR>Press Back to try again.";
// close container table
print "</TD></TR></TABLE>";

//Set up the footer of the page
PrintPageFooter("");
}

?>



hey, is there anyway i could get an email sent to me if someone signs up. I don't want any passwords or anything, just to have the user name, or even to have 'someone's signed up' or something

Phip
07-28-2002, 12:12 PM
just at the mail() to the sign up

i'm looking over your script..

Phip
07-28-2002, 12:16 PM
what is it not doing? or what orrors do you get?

Trusten
07-28-2002, 12:18 PM
Query failed at user insertion stage.


that's what it's saying. Instead of saying it's the same email address.

if i put a different email address in, then it works fine.

Phip
07-28-2002, 12:22 PM
quick lesson

wont work
$query = "SELECT user_id "

. " FROM users "

. " WHERE userid='$signupusername'";

needs to be

$query = "SELECT user_id " ;

$query .= " FROM users ";

$query .= " WHERE userid='$signupusername'";

what you have will just flatten out and $query will pretty much be destroyed and not work.

Trusten
07-28-2002, 12:26 PM
regardless of which, there must be a message to give the user, right?

Phip
07-28-2002, 12:28 PM
umm... say what? i don't understand how that relates to solving this.

Trusten
07-28-2002, 12:31 PM
no no, the sign up script works fine. no worries.

but what i'm trying to do is this


A. tell the user when he is trying to sign up with an email address that's already listed.

B. send myself an email when someone signs up

C. (as you already told me how to do) just to make sure they fill out the proper fields.

i wanted to know where in the code i should put that email script you gave me.

Phip
07-28-2002, 12:35 PM
right after else{ because the user has been validated and signed up

if ($num_rows == 1) {

create_error_page_useridinuse();

}

else {

Trusten
07-28-2002, 01:08 PM
didn't work. still the same error

if i included your script by itself, would that work?

Phip
07-28-2002, 01:11 PM
oh, i thought you said it was working...

try using or die(mysql_error()) instead of your own errors they will be a bit more descripttive i'm so tired....... *yawn*

Trusten
07-28-2002, 01:37 PM
you're assuming i'm advanced. i'm not. took me awhile to get that sign up code working andn tweaked.

i'm a beginner, sorry :(

thanks for your help though. i'm tired too.

EthanX
07-28-2002, 06:47 PM
ah, my code there :)
So I need to help :)

EthanX
07-28-2002, 06:58 PM
// query commands

mysql_select_db("$db", $linkID) or die(mysql_error());
$resultID = mysql_query("SELECT userid FROM $userstable WHERE email = '$email'", $linkID) or die(mysql_error());

// End of query commands
// Check if email already in use

$num_rows = mysql_num_rows($resultID);
$row = mysql_fetch_array($resultID);
$user_id = $row[0];

if ($user_id == "") {
//ALL THE CODE TO CREATE ONE HERE!!
}
else {
// ERROR CODE HERE!
}

EthanX
07-28-2002, 07:06 PM
<?
// User Sign Up PHP Script
//
// This script validates information, saves it to the database
// displays it to them
// and sends it to them via email
//
// vars:
// $signupusername
// $signuppassword
// $signuppasswordverify
// $signupemailaddress
//
// Note have to use signup* vars initially to make it clear to ourselves that
// we're dealing with signupotherwise

// Include common variables and message text
include 'zcommon.php';

//null out cookies if new user
//null out cookies at start of sign in routine
//all cookies begin with "ck_" to indicate that they are a cookie
//helps troubleshoot mysterious cookie errors
// note on using cookies.
// MUST BE SET before ANY http output.
// They TRAVEL in the http HEADER so have to go first.
setcookie ("ck_user", "");
setcookie ("ck_password", "");
setcookie ("ck_user_id", "");

// 0th check that passwords match

// query commands

mysql_select_db("$db", $linkID) or die(mysql_error());
$resultID = mysql_query("SELECT userid FROM $userstable WHERE email = '$email'", $linkID) or die(mysql_error());

// End of query commands
// Check if email already in use

$num_rows = mysql_num_rows($resultID);
$row = mysql_fetch_array($resultID);
$user_id = $row[0];

if ($user_id == "") {

if($signuppassword!=$signuppasswordverify) {
create_error_page_passwordsnotmatch();
}
else {
// their passwords match so enter next validation stage
// first test that their username isn't already in use

// Connecting, selecting database
$link = mysql_connect("$dbhost", "$dbuser", "$dbpassword")
or die("Could not connect");
//select the database
mysql_select_db("$db")
or die("Could not select database");
//try and select the username that the user entered to see if it is already in the db
$query = "SELECT userid "
. " FROM users "
. " WHERE userid='$signupusername'";

$result = mysql_query($query)
or die("Query failed at username unique testing stage.");

//logic -- if the num_rows is 1 then the username is already in use
// and they have to choose another
$num_rows = mysql_num_rows($result);
//don't need to get it -- its the same as what we already have

//num_rows can't ever be >= to 1 since unique constraint on the column of data
if ($num_rows == 1) {
create_error_page_useridinuse();
}
else {
// NOTE -- Depending on how you want to define
// a valid password (5 chars, 6 chars
// plus a number, etc), that would go here

//Capture the ipaddress and call MD5
$ipaddress = getenv ("REMOTE_ADDR");
$encryptedpassword = md5($signuppassword);

//try and add them to the database
$query = "INSERT INTO users "
. " ( userid, password, date, ipaddress, email, fullname, hintquestion, hintanswer ) "
. " VALUES ('$signupusername','$encryptedpassword',
NOW(), '$ipaddress', '$emailaddress', '$signupname', '$signuphint', '$signupanswer')";

//execute the query
$result = mysql_query($query)
or die("Query failed at user insertion stage.");

//now query the db back for the user_id variable
$query = "SELECT user_id "
. " FROM users "
. " WHERE userid='$signupusername'";

// get the result
$result = mysql_query($query)
or die("Query failed at userid retrieval stage.");

//get the user_id from the result
$num_rows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
$user_id = $row[0];

// send the cookies now. MUST BE FIRST THING OUTPUT
setcookie ("ck_userid", "$signupusername");
setcookie ("ck_password", "$signuppassword");
setcookie ("ck_user_id", "$user_id");

//Define the $title variable for the page title
$title = "Thanks for Signing Up!";

//Set up the page header
PrintPageHeader("$title");

//Print out the body of the page
// Note that some basic html formatting is used
// here to make it look better

print "<TABLE WIDTH=728 BGCOLOR=WHITE><TR><TD>";
print "<H1>Thank You...</H1>";
print "<HR>";
print "<CENTER>Welcome $signupname! ";
print "Hope you enjoy the ";
print "site.</CENTER><BR><BR>";
print "Here is the information that you entered:";
print "<UL>";
print "<LI>Username: $signupusername</LI>";
print "<LI>Password: $signuppassword</LI>";
print "<LI>Email Address: $emailaddress</LI>";
print "</UL>";
print "<BR>We have also emailed this to the email ";
print "address you gave us.<BR>";
print "</TD>";
print "</TR>";
print "<TABLE>";

//Usually you want to add a link to your
// application's home page here.
// Left as an exercise for the reader.

//handle sending out the email if we got an email address!
if ($emailaddress != "") {
// compose the email
$to = $emailaddress;
$subject = "Your Membership";
$message = "Welcome $signupname, "
. "

Your account has been created and is ready for use."
. ""

. ""
. "

Your username is: $signupusername"
. "
Your password is: $signuppassword"
. ""
. "

Thanks for signing up to A."
. "

<a href="http://www.me.com/" target="_blank">http://www.me.com/</a>"
. ""
. "

Jewle,
Webmaster
webmaster@me.com";
// send the email
mail($to, $subject, $message, "From: webmaster@me.com", "-
fwebmaster@me.com");
}

//Set up the footer of the page
PrintPageFooter("");
}
}

// start of functions

function PrintPageHeader ($title) {
print "<HTML>";
print "<HEAD>";
print "<TITLE>";
print "$title";
print "</TITLE>";
print "</HEAD>";
print "<BODY>";
}

function PrintPageFooter ($title) {
print "</BODY>";
print "</HTML>";
}


function create_error_page_passwordsnotmatch() {
//Define the $title variable for the page title
$title = "We're Sorry But You're Passwords Don't Match!";

//Set up the page header
PrintPageHeader("$title");

print "<TABLE WIDTH=728 BGCOLOR=WHITE><TR><TD>";
print "<H1>We're Sorry...</H1>";
print "<HR>";
print "<BR>";
print "We're sorry but You didn't enter matching passwords. ";
print "&nbsp;Please make sure that you enter your password ";
print "in the Password field and the Verify Password fields ";
print "and that both are the same.<BR><BR>";
print "Please press the back button and make sure that the ";
print "passwords match.";

// close container table
print "</TD></TR></TABLE>";

//Set up the footer of the page
PrintPageFooter("");
}


function create_error_page_useridinuse() {
//Define the $title variable for the page title
$title = "We're Sorry But that Username is Already in Use...";

//Set up the page header
PrintPageHeader("$title");

print "<TABLE WIDTH=728 BGCOLOR=WHITE><TR><TD>";

print "<H1>We're Sorry...</H1>";
print "<HR>";
print "<BR>You entered a Username that another person is using";
print "<BR>Press Back to try again.";
// close container table
print "</TD></TR></TABLE>";

//Set up the footer of the page
PrintPageFooter("");
}
}
else {
print "We're sorry but the email ". $email ." is already in use.<BR>Please go back and try another email, Thank you.";
}
?>


:thumbsup:

Trusten
07-29-2002, 08:15 AM
//Check that emails don't match
if ($emailaddess = email) {
create_error_page_emailinuse();
}
else
{



function create_error_page_emailinuse() {
//Define the $title variable for the page title
$title = "We're Sorry But that Emailaddress is Already in Use...";

//Set up the page header
PrintPageHeader("$title");

print "<TABLE WIDTH=728 BGCOLOR=WHITE><TR><TD>";

print "<H1>We're Sorry...</H1>";
print "<HR>";
print "<BR>You entered an e-mail address that another person is using";
print "<BR>Press Back to try again.";
// close container table
print "</TD></TR></TABLE>";

//Set up the footer of the page
PrintPageFooter("");
}




alright, i got a lot of help from EthanX, and i hope he won't be insulted if i ask this question, but I was curious.

see, this is what the script has originally for functions:


// 0th check that passwords match
if($signuppassword!=$signuppasswordverify) {
create_error_page_passwordsnotmatch();
}
else {
// their passwords match so enter next validation stage
// first test that their username isn't already in use

// Connecting, selecting database
$link = mysql_connect("$dbhost", "$dbuser", "$dbpassword")
or die("Could not connect");
//select the database
mysql_select_db("$db")
or die("Could not select database");
//try and select the username that the user entered to see if it is already in the db
$query = "SELECT userid "
. " FROM users "
. " WHERE userid='$signupusername'";

$result = mysql_query($query)
or die("Query failed at username unique testing stage.");

//logic -- if the num_rows is 1 then the username is already in use
// and they have to choose another

$num_rows = mysql_num_rows($result);
//don't need to get it -- its the same as what we already have

//num_rows can't ever be >= to 1 since unique constraint on the column of data

if ($num_rows == 1) {
create_error_page_useridinuse();
}

else {
// NOTE -- Depending on how you want to define
// a valid password (5 chars, 6 chars
// plus a number, etc), that would go here

//Capture the ipaddress and call MD5
$ipaddress = getenv ("REMOTE_ADDR");
$encryptedpassword = md5($signuppassword);

//try and add them to the database
$query = "INSERT INTO users "
. " ( userid, password, date, ipaddress, email, fullname, hintquestion, hintanswer ) "
. " VALUES ('$signupusername','$encryptedpassword',
NOW(), '$ipaddress', '$emailaddress', '$signupname', '$signuphint', '$signupanswer')";

//execute the query
$result = mysql_query($query)
or die("Query failed at user insertion stage.");



at the end of the script, is this


//Set up the footer of the page
PrintPageFooter("");
}
}

// start of functions

function PrintPageHeader ($title) {
print "<HTML>";
print "<HEAD>";
print "<TITLE>";
print "$title";
print "</TITLE>";
print "</HEAD>";
print "<BODY>";
}

function PrintPageFooter ($title) {
print "</BODY>";
print "</HTML>";
}


function create_error_page_passwordsnotmatch() {
//Define the $title variable for the page title
$title = "We're Sorry But You're Passwords Don't Match!";

//Set up the page header
PrintPageHeader("$title");

print "<TABLE WIDTH=728 BGCOLOR=WHITE><TR><TD>";
print "<H1>We're Sorry...</H1>";
print "<HR>";
print "<BR>";
print "We're sorry but You didn't enter matching passwords. ";
print "&nbsp;Please make sure that you enter your password ";
print "in the Password field and the Verify Password fields ";
print "and that both are the same.<BR><BR>";
print "Please press the back button and make sure that the ";
print "passwords match.";

// close container table
print "</TD></TR></TABLE>";

//Set up the footer of the page
PrintPageFooter("");
}


function create_error_page_useridinuse() {
//Define the $title variable for the page title
$title = "We're Sorry But that Username is Already in Use...";

//Set up the page header
PrintPageHeader("$title");

print "<TABLE WIDTH=728 BGCOLOR=WHITE><TR><TD>";

print "<H1>We're Sorry...</H1>";
print "<HR>";
print "<BR>You entered a Username that another person is using";
print "<BR>Press Back to try again.";
// close container table
print "</TD></TR></TABLE>";

//Set up the footer of the page
PrintPageFooter("");
}



now, this works fine, but what i wanted to do was compare the email address entered, to the email addresses in the database, and if any of them matched, an error page popped up. I know this is sort of a new topic, but i'm more concerned with functions right now, and how they work.


it's like math, i know, you are saying, if the emails match, show them this message, or 'else', let them go on.

question is, how would i write that? up to, the very top, was my own test drive. it didn't turn out well, because no matter what, well, if the passwords don't match, they would see the proper error, if the username was repeated, they would see the error message, BUT, if the email address was the same, adn if it wasn't, they would still see that 'email is used' message. so i guess i messed up.

Cloudski
07-30-2002, 11:27 AM
if($emailaddess == $email){
create_error_page_emailinuse();
}
else
{
//do whatever.....
}


Are you saything that THIS does not work?

Trusten
07-30-2002, 11:40 AM
that's what i say, it not work. at all.


but then there's this


if ($num_rows > 0) {
# this is a source code comment
# if the $num_rows variable is greater than 0 then the
# email address already exists so you need to create the error page
create_error_page_emailinuse();
}
else
{
# this is also a source code comment
# since the $num_rows variable ISN'T > than zero you need to THEN
# execute the code that adds the user to the database
# I don't have that code handy

# insert code below this line.
# You need the query line for $query = "INSERT INTO ..." to add the user to the database

}

that's what someone gave me. needless to say, it's just sitting in a file.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum