username verification part 2

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
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

username verification part 2

Post by fariquzeli »

I've tried several different things with this, and the code I currently have does not report an error message if the user enters an already in-use password, it just refreshes the form page. Here is the code I have:

Code: Select all

<? //initilize PHP
  include("webvars.inc");
  mysql_connect("$hostname","$user","$pass") or die(); //connect
  mysql_select_db("pancorp"); //select db
  
  if($submit) &#123;
  $usertest = $_POST&#1111;'username']; 
  $sql = "SELECT username FROM technicians WHERE username = '$usertest'";
  $result = mysql_query($sql) or die(mysql_error()); 
  
  if (mysql_num_rows($result) != 0) &#123; 
    echo 'Username taken, please choose another username.'; 
&#125; 
	else &#123; 

 $sql2 = "INSERT INTO technicians (id,username,password,firstname,lastname,email,location,phonenumber)".
  "VALUES ('NULL', '$username', '$password', '$firstname', '$lastname', '$email', '$location', '$phonenumber')";
  $result=mysql_query($sql2) or die(); //Insert into db
  header ("location: http://www.pancorp.com/techs/thanks.php");
		&#125; 
&#125;
mysql_close(); 
  
?>
The form is on the same page and just echoes itself.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Does it work if you use a not-already-used password?

Have you read this?

Mac
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

it works great if I have use an unused username. Form goes through fine.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Re: username verification part 2

Post by ssand »

First I should warn I am a newbie.

Does it matter that your echo statement is not in double quotes instead of single quote marks?

Or you could try:

Code: Select all

if($submit)
   &#123; 
  $usertest = $_POST&#1111;'username'];
	 $sql = "SELECT count(*) FROM technicians WHERE
					        username = '$usertest';
	 $result = mysql_query($sql) or die(mysql_error());

	 $count = mysql_result($result);
	     if ($count = 0)
		       &#123;
            echo "Username taken, please choose another username.";
		       &#125;

... etc.

Note: Untested and like I said I'm new to this.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

single quotes doesn't matter it only has one argument.

That basically will do the same thing.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

Does your submit button tag have a name=submit ?
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

$result = mysql_query($sql) or die(mysql_error());
try printing the value of mysql_num_rows to see what it is
$HowMany = mysql_num_rows($result);
print "Total number of records is $HowMany";

if ($HowMany > 0) {

echo 'Username taken, please choose another username.';
}
else {[/b]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I agree with mikeq, you really need to echo everything to find out where the problem is, that is you need to know what information you're giving and getting in order to effectively debug code so try:

Code: Select all

$usertest = $_POST&#1111;'username']; 
$sql = "SELECT username FROM technicians WHERE username = '$usertest'"; 
echo '$sql: '.$sql;  // DEBUGGING
$result = mysql_query($sql) or die(mysql_error()); 
echo '<br />num_rows: '.mysql_num_rows($result);  // DEBUGGING  
if (mysql_num_rows($result) != 0) &#123; 
    echo 'Username taken, please choose another username.'; 
&#125;
Obviously you remove all of these echo's once the problem is sorted.

Mac
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

thanks.
Post Reply