2013-04-14 234 views
0

我有一個表單,當提交時,數據將被插入到三個表(用戶,旅程,user_journey表)中。在插入數據之前,我想檢查用戶表中是否已存在該用戶。如果沒有,那麼沒有問題,用戶將被插入到用戶表中,但是,如果用戶已經存在於用戶表中,我不想再次添加該用戶。我想獲取用戶的user_id並插入到第三個表(user_journey)中。檢查行是否存在

此刻,當我提交表單時,用戶即使已經存在於表中,也會插入到用戶表中。

我不確定我去檢查用戶是否存在正確以及如何獲取user_id的方式。任何意見,將不勝感激

$query = $db->query("SELECT COUNT(*) FROM user WHERE facebook_id = '.$hdnFacebookId.'"); 

     //$query->execute(); 
     //$countRows = $query->rowCount();//return number of rows 
     //check to see if user is already in the database 

     if ($query->fetchColumn() > 0) 
     { 
      if ($oneWay) 
      { 
       $query_journey = $db->prepare("INSERT INTO journey 
       (from_destination,to_destination,journey_type,depart_date,depart_time,seats_available,journey_message,user_type) 
       VALUES('$pjFrom','$pjTo','$radioJourneyType', STR_TO_DATE('$departDate','%d/%m/%Y'),'$newDepTime','$seatcounter','$textareanotes','$radUserType')"); 
      } 
      else 
      { 
       $query_journey = $db->prepare("INSERT INTO journey 
       (from_destination,to_destination,journey_type,depart_date,depart_time,return_date,return_time,seats_available,journey_message,user_type) 
       VALUES('$pjFrom','$pjTo','$radioJourneyType', STR_TO_DATE('$departDate','%d/%m/%Y'),'$newDepTime',STR_TO_DATE('$returnDate','%d/%m/%Y'),'$newRetTime ','$seatcounter','$textareanotes','$radUserType')"); 
      } 
      $user_query = $db->prepare("SELECT user_id FROM user WHERE facebook_id = '$hdnFacebookId'"); 
      $result = $user_query->execute(); 
      $user_query_result = $user_query->fetch(PDO::FETCH_ASSOC); 

      $query_journey->execute();//EXECUTE QUERY 

      $lastJourneyID = $db->lastInsertId(); 
      $queryUserJourney = $db->prepare("INSERT INTO user_journey 
           (journey_id,user_id) 
           VALUES('$lastJourneyID','$user_query_result')"); 
      $queryUserJourney->execute();//EXECUTE QUERY 

      //include('index.php'); 
     } 
     else //insert user 
     { 
      //if $oneWay true, then omit $returnDate and $returnTime 
      if ($oneWay) 
      { 
       $query = $db->prepare("INSERT INTO journey 
       (from_destination,to_destination,journey_type,depart_date,depart_time,seats_available,journey_message,user_type) 
       VALUES('$pjFrom','$pjTo','$radioJourneyType', STR_TO_DATE('$departDate','%d/%m/%Y'),'$newDepTime','$seatcounter','$textareanotes','$radUserType')"); 
      } 
      else 
      { 
       $query = $db->prepare("INSERT INTO journey 
       (from_destination,to_destination,journey_type,depart_date,depart_time,return_date,return_time,seats_available,journey_message,user_type) 
       VALUES('$pjFrom','$pjTo','$radioJourneyType', STR_TO_DATE('$departDate','%d/%m/%Y'),'$newDepTime',STR_TO_DATE('$returnDate','%d/%m/%Y'),'$newRetTime ','$seatcounter','$textareanotes','$radUserType')"); 
      } 
      $queryfb = $db->prepare("INSERT INTO user 
       (facebook_id,facebook_username,facebook_first_name,facebook_last_name,facebook_image,facebook_link) 
       VALUES('$hdnFacebookId','$hdnUsername','$hdnFirstName','$hdnLastName','$hdnFacebookImg','$hdnFacebookUrl')"); 

      $query->execute(); 
      $lastUserID = $db->lastInsertId(); 
      $queryfb->execute(); 
      $lastJourneyID = $db->lastInsertId(); 
      $queryUserJourney = $db->prepare("INSERT INTO user_journey 
           (user_id,journey_id) 
           VALUES('$lastJourneyID','$lastUserID')"); 
      $queryUserJourney->execute(); 

     } 

修訂

function userExists($db, $hdnFacebookId) 
     { 
      $userQuery = "SELECT * FROM user WHERE facebook_id = :user;"; 
      $stmt = $db->prepare($userQuery); 
      $stmt->execute(array(':user'=>$hdnFacebookId)); 
      $result = $stmt->fetch(PDO::FETCH_ASSOC); 
      if($result) 
      { 
       return true; 
      } 
      return false; 
     } 

     $userExists = userExists($db,$hdnFacebookId); 
     if($userExists) 
     { 
      //don't insert user 
      //get user's id from database 
      $user_query = $db->prepare("SELECT * FROM user WHERE facebook_id = '$hdnFacebookId'"); 
      $result = $user_query->execute(); 
      $user_query_result = $user_query->fetch(PDO::FETCH_ASSOC); 
      $userID = $user_query_result['user_id']; 
      $query_journey->execute();//EXECUTE QUERY 
      $lastJourneyID = $db->lastInsertId(); 
      $queryUserJourney = $db->prepare("INSERT INTO user_journey 
           (journey_id,user_id) 
           VALUES('$lastJourneyID','$userID')"); 
      $queryUserJourney->execute();//EXECUTE QUERY 
     } 
     else 
     { 
      //insert user 
     } 

回答

3

一個典型的 「檢查用戶是否存在」:

function userExists($db, $user) 
{ 
    $userQuery = "SELECT * FROM users u WHERE u.user=:user;"; 
    $stmt = $db->prepare($userQuery); 
    $stmt->execute(array(':user' => $user)); 
    $result = $stmt->fetch(PDO::FETCH_ASSOC); 
    if($result) 
    { 
     return true; 
    } 
    return false; 
} 

所以,你可以這樣做

$user = isset($_POST['user']) ? $_POST['user'] : "Unknown"; 
$userExists = userExists($db, $user); 
if($userExists) 
{ 
    // Don't insert 
] 
else 
{ 
    // Insert the user. 
} 
+2

或者更簡單地說,'return !! $ stmt-> fetch(PDO :: FETCH_ASSOC);' –

+0

當然,它可以工作,我只是發現一些代碼比別人漂亮,但謝謝你的提示。即使它是一個關聯數組(它是),它也可以工作嗎? – Jonast92

+1

它的工作方式與您發佈的代碼完全相同。 –