2017-05-27 199 views
1

我想實現的功能,其中它會檢查是否如果用戶在我的數據庫已經存在,是它會插入所有註冊數據,但它似乎沒有工作=(可能有人請幫我找出錯誤所在。真的很感謝所有的答案提前。PHP檢查用戶是否存在PDO

<?php 

require '../ppuyakul/php/db_conn.php'; 


$message = ''; 

//Prepare date 
$DOB = date("Y-m-d", strtotime($_POST['year'].'-'. $_POST['month'].'-'. $_POST['day'])); 
$accessType = "0"; 

//Check enpty field 
if(!empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['fullname']) && !empty($_POST['username']) && !empty($_POST['password_confirmation']) && !empty($_POST['gender']) && !empty($_POST['country']) && !empty($_POST['state']) && !empty($_POST['city']) && !empty($_POST['day']) && !empty($_POST['month']) && !empty($_POST['year'])): 

    // Enter the new user in the database 
    $sql = "INSERT INTO assignment2 (fullname, username, email, password, gender, country, state, city, DOB, type) VALUES (:fullname, :username, :email, :password, :gender, :country, :state, :city, :DOB, :type)"; 
    $stmt = $conn->prepare($sql); 

    $stmt->bindParam(':fullname', $_POST['fullname']); 
    $stmt->bindParam(':username', $_POST['username']); 
    $stmt->bindParam(':email', $_POST['email']); 
    $stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT)); 
    $stmt->bindParam(':gender', $_POST['gender']); 
    $stmt->bindParam(':country', $_POST['country']); 
    $stmt->bindParam(':state', $_POST['state']); 
    $stmt->bindParam(':city', $_POST['city']); 
    $stmt->bindParam(':DOB', $DOB); 
    $stmt->bindParam(':type', $accessType); 

    $chk = $conn->prepare("SELECT username FROM assignment2 WHERE username = :name"); 
    $chk->bindParam(':name', $username); 
    $chk->execute(); 

    if($chk->rowCount() > 0): 
    $message = 'Error ! ! User already exists'; 
    else: 
     if($stmt->execute()): 
     $message = 'Successfully created new user'; 
      else: 
      $message = 'Sorry there must have been an issue creating your account'; 
      endif; 
     endif; 
    endif; 
?> 
+1

$ username是如何設置的? –

+0

嗨Paul感謝so muc h爲您的回覆,我嘗試在這裏聲明 '$ all_rows = $ chk-> fetchAll(PDO :: FETCH_ASSOC); 的foreach($ ALL_ROWS爲$行): $用戶名= $行[ 「用戶名」]; $ _GLOBAL ['username'] = $ username; endforeach;' 之前'如果($ chk-> rowCount時()> 0):' 但仍沒有運氣 –

+1

我看不出有什麼最初發布的代碼?當我搜索$ username時(在我的第一條評論之前),這個頁面上只有一個'點擊'。上面的代碼中沒有foreach。爲你的rowCount檢查添加一個else,並打印'找不到行'(或其他),看看你得到了什麼。你也可以使用$ username來查看輸出結果。 –

回答

1

據@保羅T.我終於發現這裏的解決方案是最後的代碼,感謝這麼多再次爲你的幫助@ Paul T.

$username = $_POST['username']; 
    $chk = $conn->prepare("SELECT username FROM assignment2 WHERE username = :name"); 
    $chk->bindParam(':name', $username); 
    $chk->execute(); 


    if($chk->rowCount() > 0): 
    $message = 'Error ! ! User already exists'; 
    else: 
     if($stmt->execute()): 
     $message = 'Successfully created new user'; 
      else: 
      $message = 'Sorry there must have been an issue creating your account'; 
      endif; 
     endif; 
    endif; 
+1

你也可以簡單地在綁定中使用$ _POST ['username']引用來避免額外的變量需求。你發現了我所得到的,但是沒有設置$ username。 –

+0

@PaulT。啊,我看到^^「再次感謝。 –