2013-03-20 55 views
-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'); 
} 

任何幫助將不勝感激

回答

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 
    } 
} 
+0

非常感謝是一個很大的幫助 – user2162672 2013-03-20 23:33:55