Page 1 of 1

username verification part 2

Posted: Mon Jun 24, 2002 10:51 am
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.

Posted: Tue Jun 25, 2002 2:06 am
by twigletmac
Does it work if you use a not-already-used password?

Have you read this?

Mac

Posted: Tue Jun 25, 2002 9:20 am
by fariquzeli
it works great if I have use an unused username. Form goes through fine.

Re: username verification part 2

Posted: Tue Jun 25, 2002 10:11 am
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.

Posted: Tue Jun 25, 2002 10:13 am
by fariquzeli
single quotes doesn't matter it only has one argument.

That basically will do the same thing.

Posted: Tue Jun 25, 2002 10:18 am
by ssand
Does your submit button tag have a name=submit ?

Posted: Tue Jun 25, 2002 12:46 pm
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]

Posted: Wed Jun 26, 2002 2:24 am
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

Posted: Wed Jun 26, 2002 9:14 am
by fariquzeli
thanks.