dongwang788787 2012-11-21 00:05
浏览 21

检查是否已使用用户名

i need to check if the username is taken and if it is it wont allow the user to register it will redirect them to the register page where should i add the script to check if the username is taken alredy? or where can i get a script for this heres my script what i have sofar.

 <?php

// Check if he wants to register:
if (!empty($_POST[username]))
{
// Check if passwords match.
if ($_POST[password] != $_POST[password2])
    exit("Error - Passwords don't match. Please go back and try again.");

// Assign some variables.
$date = time (" d - m - Y ");
$ip = $_SERVER[REMOTE_ADDR];

require_once("connect.php");

// Register him.
$query = mysql_query("INSERT INTO members 
(username, fname, email, password, date, ip)
VALUES      ('$_POST[username]','$_POST[fname]','$_POST[email]','$_POST[password]','$date','$ip')")
or die ("Error - Couldn't register user.");

echo "Welcome $_POST[username]! You've been successfully reigstered!<br />
    You Will Be Redirected To Our Home Page Where U Can Login ";
exit();
}

?>
  • 写回答

3条回答 默认 最新

  • doushengyou2617 2012-11-21 00:08
    关注

    You need to check the presence of your username first:

    SELECT count(1) FROM members WHERE username = '$_POST[username]'
    

    And then if the result is > 0 you throw a User already exists error.

    评论

报告相同问题?