Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking if name exists

Status
Not open for further replies.

calv1n

Programmer
Sep 29, 2007
18
CA
I added this to check if username exists when adding administrator on my site:

Code:
                $result = mysql_query("SELECT username from admin where username='$username'");
                list($usernametocheck) = mysql_fetch_row($result);

                if (!$usernametocheck) {
                        //good!
                } else {
                        Message("Username Exists", "The username you have chosen already exists.", "");
                }

Okay so I added that to the 'edit admin' function and I get the message 'The username you have chosen already exists.'. I think I know why, but I can;t fix it.

Any help will be appreciated.

-CALV1N
 
Try this code:
Code:
mysql_connect($server, $db_user, $db_pass) or die ("CONNECT Error");


    // gather link
    $result=mysql_db_query($database, "SELECT username FROM `admin` WHERE `username` = '{$username}'") or die("Database SELECT Error");
    $c_totals  = mysql_num_rows($result);

    if ($c_totals > 0)  {
	Message("Username Exists", "The username you have chosen already exists.", "");
	    /*
	      while ($row = mysql_fetch_array($result)) {
	        } // end while
	    */
      } // end if $c_totals > 0
    else {
    	echo "\n\n\t<!-- Added: {$username} -->\n\n";
      }

Ps. I wrote it in textpad and havent tried it..
It's also worth mentioning you should secure the variables..

eg.. Look at mysql injection (best practice, safe queries).
Use sprintf, strip_tags, trim, etc. on the variables!

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top