2015-08-19 135 views
2

我目前正在一個項目上工作,並且我有用於插入的表格。我的表格被稱爲survey,字段是id,用戶名,密碼,省。用戶名被設置爲唯一密鑰。插入過程中沒有任何重複的條目工作正常,但是當我嘗試插入重複條目在始終顯示我這個錯誤如何使用PDO檢查用戶名是否已經存在?

SQLSTATE [23000]:完整性約束違規:1062重複條目「虛張聲勢」關鍵「用戶名「

我知道這是什麼意思的錯誤,我的問題是,如何我可以,如果用戶名已經存在或不是我想要的警報消息彈出..

這裏是我的代碼

class.user.php

public function username($username){ 
$stmt = $this->db->prepare("SELECT count(*) FROM tish_images WHERE username = :username"); 
$stmt->execute(array($username)); 
$number_of_rows = $result->fetchColumn(); 
if($number_of_rows >= 1) { 
    echo 'username does exist'; // or return so you get the value 
} else { 
    echo 'username does not exist'; //also return? 
} 
} 
public function create($username,$password,$province) 
{ 
try 
{ 
    $stmt = $this->db->prepare("INSERT INTO tish_images(username,password,province) VALUES(:username, :password, :province)"); 
    $stmt->bindparam(":username",$username); 
    $stmt->bindparam(":password",$password); 
    $stmt->bindparam(":province",$province); 
    $stmt->execute(); 
    return true; 
} 
catch(PDOException $e) 
{ 
    echo $e->getMessage(); 
    return false; 
} 
} 

的index.php

<?php 
include_once 'DB.php'; 

$username = isset($_GET['username']) ? $_GET['username'] : ''; 
$password = isset($_GET['password']) ? $_GET['password'] : ''; 
$province = isset($_GET['province']) ? $_GET['province'] : ''; 

if(isset($_FILES['files'])){ 
$id = $_GET['id']; 
$username = $_POST['username']; 
$password = $_POST['password']; 
$province = $_POST['province']; 

if($crud->upload($id,$FILE_NAME,$FILE_SIZE,$FILE_TYPE,$username,$password,$province)) 
{ 
    echo "<script type='text/javascript'>alert('Successfully Updated!');</script>"; 
} 
else 
{ 
    echo "<script type='text/javascript'>alert('Updating Failed!');</script>"; 
} 

} 
if(isset($_GET['id'])) 
    { 
    $id = $_GET['id']; 
    extract($crud->getID($id)); 
    } 
+1

插入之前運行一個選擇。如果您有值提示用戶選擇另一個名稱。如果不插入。你也不應該以明文存儲密碼。 – chris85

+2

另一種選擇是簡單地捕捉異常並在那裏顯示/觸發警報。看到這些答案的例子〜http://stackoverflow.com/a/21869475/283366和http://stackoverflow.com/a/21618269/283366 – Phil

+0

@ chris85讓我更新我的代碼,因爲我試圖做到這一點,但仍然沒有工作 – kier

回答

0

要檢查用戶名或電子郵件已經存在。我在那裏添加了電子郵件,因爲這也很有用。您不希望兩個用戶使用相同的電子郵件地址。那麼我不會看到它的需要。 :)

完整的代碼添加和最新。

 $query_check_user_name = $this->db_connection->prepare('SELECT user_name, user_email FROM users WHERE user_name=:user_name OR user_email=:user_email'); 
     $query_check_user_name->bindValue(':user_name', $user_name, PDO::PARAM_STR); 
     $query_check_user_name->bindValue(':user_email', $user_email, PDO::PARAM_STR); 
     $query_check_user_name->execute(); 
     $result = $query_check_user_name->fetchAll(); 
     if ($result > 0) { 
      echo "Someone with that username/email already exists."; 
     } else { 
      //Continue with proccessing the form 
     } 

OR

  $query_check_user_name = $this->db_connection->prepare('SELECT user_name, user_email FROM users WHERE user_name=:user_name OR user_email=:user_email'); 
     $query_check_user_name->bindValue(':user_name', $user_name, PDO::PARAM_STR); 
     $query_check_user_name->bindValue(':user_email', $user_email, PDO::PARAM_STR); 
     $query_check_user_name->execute(); 
     $result = $query_check_user_name->fetchAll(); 
     if ($result > 0) { 
      return true; 
     } else { 
      return false; 
     } 
+1

'if(count($ result)> 0){'---這個檢查是多餘的 – zerkms

+0

我一直是使用'SELECT 1 ...'查詢,然後使用'$ stmt-> fetchColumn )' – Phil

+0

@丹尼爾我究竟把它放在哪裏? – kier

0

你應該執行查詢,看是否存在用戶名之前運行SELECT

// count how many rows with user name exists 
$checkUserStmt = $this->db->prepare(" 
    SELECT count(1) 
    FROM tish_images 
    WHERE username = :username 
"); 
$checkUserStmt->execute(array(":username" => $username)); 

// fetch the count result 
if ($checkUserStmt->fetchColumn() > 0) { 
    // username already exists 
} else { 
    // username available 
} //if 

一些注意事項。

+0

我在哪裏準確地放它?因爲我只是新的PDO,我怎麼稱它在我的index.php – kier

相關問題