Page 1 of 1
Registration Question
Posted: Tue Oct 21, 2008 5:21 pm
by deepermethod
I am somewhat new to php/mysql so bear with me.
I have a registration form when a new user signs up. I have a field that a new member MUST fill in. The field is a "member referral" field. What I need to know is how can I check my database to verify the current members username actually exists before the new member can continue registration? If the members username is there continue. If the username is not a current member show an error.
Sorry of this seems confusing. I can post the code if needed.
Thanks.
Re: Registration Question
Posted: Tue Oct 21, 2008 5:24 pm
by onion2k
Just do a SELECT with the username in the WHERE clause, and check to see if it returns any results. If it does then the username exists.
Re: Registration Question
Posted: Tue Oct 21, 2008 5:29 pm
by deepermethod
Sorry if I don't understand. This script is not mine, I am modifying it.
Scroll down to where I posted:
//here is where I want to verify that the new member has entered a valid current members username//.
Here is the whole register.inc.php script:
Code: Select all
$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");
$checkl = "SELECT * FROM $tbl_members WHERE login = ".quote_smart($login)."";
$check_l = @mysql_query($checkl,$connection) or die("Couldn't execute login check query.");
while ($row = mysql_fetch_array($check_l)) {
$ch_login = $row['login'];
}
if(isset($ch_login)) {
$login_err = "That username is already taken!";
$error = "1";
}
$check2 = "SELECT * FROM $tbl_restricted WHERE r_login = ".quote_smart($login)."";
$check_2 = @mysql_query($check2,$connection) or die("Couldn't execute login check query.");
while ($row = mysql_fetch_array($check_2)) {
$r_login = $row['r_login'];
}
if(isset($r_login)) {
$login_err = "That username is reserved!";
$error = "1";
}
[size=150][color=#FF0000]//here is where I want to verify that the new member has entered a valid current members username//[/color][/size]
$checke = "SELECT * FROM $tbl_members WHERE email = ".quote_smart($email)."";
$check_e = @mysql_query($checke,$connection) or die("Couldn't execute email check query.");
while ($row = mysql_fetch_array($check_e)) {
$ch_email = $row['email'];
}
if(isset($ch_email)) {
$email_err = "That email address has already been used!";
$error = "1";
}
if(empty($newsletter)) $newsletter = "no";
if(!$error) {
//input is ok, register new member!
$ipaddr = $_SERVER['REMOTE_ADDR'];
$sql = "INSERT INTO $tbl_members (login, enabled, password, email, displayname, newsletter, ipaddr, deferral)
VALUES (
".quote_smart($login).",
".quote_smart($autoenable).",
".quote_smart($pass1).",
".quote_smart($email).",
".quote_smart($displayname).",
".quote_smart($newsletter).",
".quote_smart($ipaddr).",
".quote_smart($deferral).",
)";
$result = @mysql_query($sql,$connection) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
if($mailonnew == "yes") {
$to = "$site_email";
$subject = "New $sitename Member!";
$from_mail = "$adminemail";
$message = "Dear Admin,\n\n";
$message .= "You have a new member with the login: $login.\n\n";
$message .= "$siteurl";
$headers = "From: $from_mail\r\n";
$headers .= "Reply-To: $from_mail\r\n";
$headers .= "X-Mailer: phpProfiles";
mail($to, $subject, $message, $headers);
}
include("./include/welcome_msg.inc.php");
include("./include/reg_success.inc.php");
exit;
}
$ipaddr = $_SERVER['REMOTE_ADDR'];
$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");
$sql = "SELECT * FROM $tbl_banned WHERE bip = \"$ipaddr\"";
$result = @mysql_query($sql,$connection) or die("Couldn't execute ban lookup query.");
$num=mysql_num_rows($result);
if($num > 0) {
while ($row = mysql_fetch_array($result)) {
$bcomment = $row['bcomment'];
}
if(empty($bcomment)) {
$bcomment = "no reason given.";
}
echo "<p>Sorry, you cannot register at this time. You have been banned because:</p>
<p align=\"center\"><i>$bcomment</i></p>";
include("./include/footer.inc.php");
exit;
}
if (!$_POST) { ?>
Re: Registration Question
Posted: Wed Oct 22, 2008 3:52 am
by aceconcepts
So on line 22 you will write a query that checks the database, just like onion2k said.
You already have smaple queries in your script which you can modify. You will most likely make use of mysql_num_rows() or COUNT() so it won't be that taxing.
The basic structure might look something like this:
Code: Select all
$username_var=$_POST['username_var'];
$checkUname=mysql_query("SELECT username FROM table_name WHERE username='$username_var'");
if(mysql_num_rows($checkUname)>0)
{
//username exists
} else {
//username does not exist
}