Check if someone is already registered...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Random
Forum Commoner
Posts: 30
Joined: Wed Mar 12, 2003 5:38 pm

Check if someone is already registered...

Post by Random »

Hello, what is the lines that I need to output "Username already registered" for a signup form? Thanks.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Code: Select all

<p>Username already registered.</p>
I'm assuming you're looking for a bit more code than this...

Basically you need to test to see if the username is in the database (or flat file) and if it is you know that it's been registered and if it isn't then you know it hasn't.

Mac
Random
Forum Commoner
Posts: 30
Joined: Wed Mar 12, 2003 5:38 pm

Post by Random »

yes that is basically what i asked, how do u write it?
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

you might give us some details
Random
Forum Commoner
Posts: 30
Joined: Wed Mar 12, 2003 5:38 pm

Post by Random »

okay, i thought it would only be a line. I will also post my scripts.

I want it so someone registers, and the username is then input into the database and no one else can use that username. Now I can hit refresh, resend the data, and enter another row with the same info. So basically I can have an unlimited amount of usernames called "Random", which I don't want. I know it will probably be an If statement, I don't know how to set it up though. Here are my scripts:

FORM:

Code: Select all

&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;
&lt;h1&gt;Please signup!&lt;/h1&gt;
&lt;p align="center"&gt;
&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;
&lt;Table border=0&gt;
&lt;form action="signup_process.php" method="post"&gt;
&lt;tr valign=top&gt;
&lt;th align=right&gt;Desired Username:&lt;td&gt; &lt;input type="text" name="username" maxlength=20&gt;&lt;BR&gt;
&lt;tr valign=top&gt;
&lt;th align=right&gt;Desired Password:&lt;td&gt; &lt;input type="password" name="password" maxlength=20&gt;&lt;BR&gt;
&lt;tr valign=top&gt;
&lt;th align=right&gt;First Name:&lt;td&gt; &lt;input type="text" name="f_name" maxlength=25&gt;&lt;BR&gt;
&lt;tr valign=top&gt;
&lt;th align=right&gt;Last Name:&lt;td&gt; &lt;input type="text" name="l_name" maxlength=25&gt;&lt;BR&gt;
&lt;tr valign=top&gt;
&lt;th align=right&gt;Email Address:&lt;td&gt; &lt;input type="text" name="email" maxlength="60"&gt;&lt;BR&gt;
&lt;tr valign=top&gt;
&lt;th align=right&gt;Age:&lt;td&gt; &lt;input type="text" name="age" maxlength="3"&gt;&lt;BR&gt;
&lt;tr valign=top&gt;
&lt;th align=right&gt;Gender:&lt;td&gt; Male&lt;input type="radio" name="male" value="male"&gt;  Female&lt;input type="radio" name="female" value="female"&gt;&lt;BR&gt;&lt;/td&gt;
&lt;/table&gt;
&lt;p align="center"&gt;
&lt;input type="submit" value="Finish signup!"&gt;
PROCESS THE FORM:

Code: Select all

<?php
$dbuser = "random";
$dbpass = "random";
$dbname = "random";
$uname = $_POST['username'];
$pword = $_POST['password'];
$fname = $_POST['f_name'];
$lname = $_POST['l_name'];
$email = $_POST['email'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$to = $_POST['email'];
$subject = "Thank you for signing up!";
$link = mysql_connect("localhost", $dbuser, $dbpass) or die ("Database Error: Couldn't Connect!");
$message = "Thank you for signing up, $fname!  Your account has been entered into our database.

Username: $uname 
Password: $pword 

PLEASE DO NOT REPLY TO THIS EMAIL.";
$addy = "From:  no-reply@travisbsd.no-ip.org";

mysql_select_db($dbname, $link) or die ("Couldn't open $db!");

$query = "INSERT INTO user_info (username, password, f_name, l_name, email, age, gender)
	values( '$uname', '$pword', '$fname', '$lname', '$email', '$age', '$gender' )";

mysql_query($query, $link) or die ("Couldn't input data!");


mysql_close($link);
print "<BR>
<BR>
<BR>
";
print "Thanks for signing up!";

mail($to, $subject, $message, $addy);
?>
Thanks in advance.
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

ah its easy now - before we didnt know the structure of your db or anything. how are we to guess? anyway...

Code: Select all

...
mysql_select_db($dbname, $link) or die ("Couldn't open $db!");

$query="select uname from user_info were username='$uname'";
$result=mysql_query($query);
if (mysql_num_rows($result)==0) &#123;
  $query = "INSERT INTO user_info (username, password, f_name, l_name, email, age, gender)
   values( '$uname', '$pword', '$fname', '$lname', '$email', '$age', '$gender' )";

  mysql_query($query, $link) or die ("Couldn't input data!");
&#125; else &#123; // 
  print "Username already taken";
&#125;
mysql_close($link); 
...
Random
Forum Commoner
Posts: 30
Joined: Wed Mar 12, 2003 5:38 pm

Post by Random »

It is saying line 28 has an error, which

Code: Select all

<? $result=mysql_query($query); 
if (mysql_num_rows($result)==0) { 
  $query = "INSERT INTO user_info (username, password, f_name, l_name, email, age, gender) 
?>
is somewhere in there.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Which is line 28, what does the error message say exactly and could you show us lines 20-28 so we can help you debug.

Mac
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

its missing a quote off the end

Code: Select all

$query = "INSERT INTO user_info (username, password, f_name, l_name, email, age, gender)
?>

Code: Select all

$query = "INSERT INTO user_info (username, password, f_name, l_name, email, age, gender)";
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

d1223m wrote:its missing a quote off the end

Code: Select all

$query = "INSERT INTO user_info (username, password, f_name, l_name, email, age, gender)
?&gt;

Code: Select all

$query = "INSERT INTO user_info (username, password, f_name, l_name, email, age, gender)";
?&gt;
Not if they've just copied and pasted the other code you posted:

Code: Select all

$query = "INSERT INTO user_info (username, password, f_name, l_name, email, age, gender) 
   values( '$uname', '$pword', '$fname', '$lname', '$email', '$age', '$gender' )";
I just assumed they weren't showing us all the code - of course it may have been that they didn't copy the second line across.

Mac
Random
Forum Commoner
Posts: 30
Joined: Wed Mar 12, 2003 5:38 pm

Post by Random »

line 28 is the num_rows == 0 line, and here is the error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /usr/home/random/pubhtml/signup_process.php on line 28

Again, line 28 is this:

if (mysql_num_rows($result)==0) {
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Code: Select all

<?php 
$dbuser = "random"; 
$dbpass = "random"; 
$dbname = "random"; 
$uname = $_POST['username']; 
$pword = $_POST['password']; 
$fname = $_POST['f_name']; 
$lname = $_POST['l_name']; 
$email = $_POST['email']; 
$age = $_POST['age']; 
$gender = $_POST['gender']; 
$to = $_POST['email']; 
$subject = "Thank you for signing up!"; 
$link = mysql_connect("localhost", $dbuser, $dbpass) or die ("Database Error: Couldn't Connect!"); 
$message = "Thank you for signing up, $fname!  Your account has been entered into our database. 

Username: $uname 
Password: $pword 

PLEASE DO NOT REPLY TO THIS EMAIL."; 
$addy = "From:  no-reply@travisbsd.no-ip.org"; 

$dbase = mysql_select_db($dbname, $link) or die ("Couldn't open $db!"); 

$find_username = mysql_query("SELECT `username` FROM `user_info` WHERE `username` = '$uname'");
if (mysql_num_rows($find_username) != 0) {
   die("Username has already been taken.");
} else {
   $sql = mysql_query("INSERT INTO `user_info` (`username`, `password`, `f_name`, `l_name`, `email`, `age`, `gender`) 
   VALUES ('$uname', '$pword', '$fname', '$lname', '$email', '$age', '$gender')", $link) or die ("Couldn't input data!");


mysql_close($link); 
print "<BR> 
<BR> 
<BR> 
"; 
print "Thanks for signing up!"; 

mail($to, $subject, $message, $addy); 
}
?>
Image Image
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The query is failing (or the db connection or selection), you need to add some error handling so you can see what's really going wrong. It looks like there's already some or die() statements on the mysql_connect() and mysql_select_db() calls but you really need something on your mysql_query call, so that instead of:

Code: Select all

$result=mysql_query($query);
you have

Code: Select all

$result=mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');
Then you may find that the error MySQL reports (using mysql_error()) will enable you to solve the problem (if not take a good look at phice's code because the answer is in there).

Mac
Post Reply