2015-02-11 91 views
0

好的,我有一個問題。我需要檢查用戶是否已經存在,但問題是現在我需要在if()中輸入我無法獲取,因爲我已經關閉,但如果我沒有關閉,我得到一個錯誤,因爲無法運行2條語句。所以我想如果有人能幫助我?我有其他代碼,但我只在這裏給代碼。Mysqli編寫語句寄存器

這裏是我的代碼:

$result = $mysqli->prepare("SELECT username FROM user WHERE username=?"); 
$result->bind_param("s", $username); 
$result->execute(); 
$result->bind_result($username);         
$result->close(); 

if(){ 
$register = $mysqli->prepare("INSERT INTO user 
        (username, password, email, rr, rank) 
        VALUES (?, ?, ?, ?, ?)"); 
$register->bind_param("sssii", $username, $kode, $email, $rr, $rank); 
$register->execute(); 
$register->close();         
} else { 
    echo "User already exists!"; 
} 

回答

0

更新: 「!用戶已經存在」 更符合邏輯的說法

$result = $mysqli->prepare("SELECT username FROM user WHERE username=?"); 
$result->bind_param("s", $username); 
$result->execute(); 
$found = $result->fetch();       
$result->close(); 

if ($found){   
    echo "User already exists!";           
} else { 
    $register = $mysqli->prepare("INSERT INTO user 
        (username, password, email, rr, rank) 
        VALUES (?, ?, ?, ?, ?)"); 
    $register->bind_param("sssii", $username, $kode, $email, $rr, $rank); 
    $register->execute(); 
    $register->close(); 

} 
+0

它使用戶名的合法性及其saing回聲;並把數據同名,但如果它不存在,它沒有說什麼,並沒有把數據在 – 2015-02-11 19:13:52

+0

這是不可能的,它不能做的事情從if和else .... – arnoudhgz 2015-02-11 19:18:33

+0

我該怎麼做? – 2015-02-11 19:19:37