-2

这是我迄今为止注册用户的代码。

现在我需要:

  • 如果用户名已被占用,则返回错误消息
  • 如果字段为空,则显示错误消息

这是代码:

//=============Configuring Server and Database=======
$host        =    'localhost';
$user        =    'root';
$password    =    'revilo';
//=============Data Base Information=================
$database    =    'dbsneaker';

$conn        =    mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
mysql_select_db($database,$conn) or die('Database Information is not correct');

//===============End Server Configuration============

//=============Starting Registration Script==========

$userName    =    mysql_real_escape_string($_POST['txtUser']);

$password    =    mysql_real_escape_string($_POST['txtPassword']);


if(isset($_POST['btnRegister'])) //===When I will Set the Button to 1 or Press Button    to register
{
$query    =    "insert into bladmin(admin_usr_name, admin_pwd) values('$userName', '$password')";
$res    =    mysql_query($query);
header('location:success_register.php');
}

任何帮助,将不胜感激

4

1 回答 1

1

我认为这会有所帮助...

// Validate empty
if(!trim($userName) || !trim($password)){
    // Some field still empty
}else{
    // There's no empty field
    // Check user exists
    $checkQuerySql = "SELECT * FROM `bladmin` WHERE `admin_usr_name` = '$userName'";
    $checkQuery = mysql_query($checkQuerySql);
    if(mysql_fetch_assoc($checkQuery)){
        // User exists
    }else{
        // User doesnt exists
    }
}
于 2013-03-20T23:26:30.707 回答