2016-06-07 22 views
0

我想要做的是1個用戶只能在1個房間,如果他想第二個房間,他必須離開以前。sql語句檢查用戶在任何房間

我面對的是我可以在任何房間,如果我重新進入任何房間的模態消息流行。看起來像我的聲明會出錯==我有這個難的時候。任何人都可以指出我有錯嗎?

的表名作爲chatroom_user cloumns = chatroom_id | user_id

chatroom_id is link with chatroom table 
user_id is link with user table 

這是我的代碼

<?php 

include '../config.php'; 

$id=$_SESSION['id']; 
$chatroomid=$_GET['chatroomID']; 

$chatroomname=mysqli_query($connection,"SELECT name from chatroom Where id='$chatroomid'"); 
$getchatroomname= mysqli_fetch_assoc($chatroomname); 

$check_data= mysqli_query($connection, "SELECT * FROM chatroom_user WHERE chatroom_id='$chatroomid' AND user_id='$id'")or die(mysqli_error($connection)); 
$check= mysqli_num_rows($check_data); 

$checkUserDataInAnyRoom = mysqli_query($connection, "SELECT * FROM chatroom_user WHERE chatroom_id='2' AND user_id=1 "); 
$checkUser= mysqli_num_rows($checkUserDataInAnyRoom); 

if($check==0){ 

    $update_status="INSERT INTO chatroom_user (`chatroom_id`,`user_id`)VALUES ('$chatroomid','$id')"; 
    $check_status=mysqli_query($connection,$update_status)or die(mysqli_error($connection)); 

    $count=mysqli_query($connection,"SELECT * from chatroom_user WHERE chatroom_id ='$chatroomid' ")or die(mysqli_error($connection)); 

    $total_user_in_any_chatroom = mysqli_num_rows($count); 

     echo " Total User : ". $total_user_in_any_chatroom. " " ; 
    } 
    elseif ($checkUser == 0) { 
     echo "<script>$(document).ready(function(){ $('#remind-user-chatroom').modal('show'); });</script> 
       <a href='chatrooms.php'> 
        <div class ='modal fade' id ='remind-user-chatroom' role ='dialog'> 
         <div class ='modal-dialog'> 
          <div class ='modal-content'> 
           <div class ='modal-header'> 
            <button type='class' class ='close' data-dismiss ='modal'>&times;</button> 
            <label class ='modal-title'>Reminder</label> 
           </div> 
           <div class ='modal-body'> 
            <span>You haven't leave previous chatroom. Please leave it and re-enter back. Thank You</span> 
             <div class ='submit-container'> 
              <button type='button' class='btn content-btn'>Leave</button> 
             </div> 
           </div> 
          </div> 
         </div> 
        </div> 
       </a>"; 
    } 
    else{ 
     $count=mysqli_query($connection,"SELECT * from chatroom_user WHERE chatroom_id ='$chatroomid' ")or die(mysqli_error($connection)); 

    $total_user_in_any_chatroom = mysqli_num_rows($count); 

     echo " Total User : ". $total_user_in_any_chatroom. " " ; 
    } 

?>

+0

是否有錯誤訊息?並轉義數據或你會得到SQL注入問題 – user489872

+0

我沒有得到任何錯誤消息==這是主要問題,我不能找到哪個部分是問題或者也許我的邏輯思維是沒有那麼好== – Chew

回答

0

變化

"SELECT name from chatroom Where id='$chatroomid'" 
"SELECT * FROM chatroom_user WHERE chatroom_id='$chatroomid' AND user_id='$id'" 
"INSERT INTO chatroom_user (`chatroom_id`,`user_id`)VALUES ('$chatroomid','$id')" 
"SELECT * from chatroom_user WHERE chatroom_id ='$chatroomid' " 

"SELECT name from chatroom Where id='".$chatroomid."'" 
"SELECT * FROM chatroom_user WHERE chatroom_id='".$chatroomid."' AND user_id='".$id."'" 
"INSERT INTO chatroom_user (`chatroom_id`,`user_id`)VALUES ('".$chatroomid."','".$id."')" 
"SELECT * from chatroom_user WHERE chatroom_id ='".$chatroomid."' "