2012-04-26 46 views
0

我在下面的腳本中掃描允許查看帖子的用戶。我如何更新它,以便將查看者的ID與存儲在該字段中的人匹配。如果它匹配,那麼它不工作。存儲的條目將類似於99394david,324234smith,34343jane。所以這個腳本我沒有匹配它。如何匹配來自多個ID的一個ID?

$kit = mysql_real_escape_string($_GET['id']); 

$sql="SELECT `Who_can_see` from `posts` where `post_id` = '$kit'"; 
    $result=mysql_query($sql); 

    $query = mysql_query($sql) or die ("Error: ".mysql_error()); 

    if ($result == "") 
    { 
    echo ""; 
    } 
    echo ""; 


    $rows = mysql_num_rows($result); 

    if($rows == 0) 
    { 
    print(""); 

    } 
    elseif($rows > 0) 
    { 
    while($row = mysql_fetch_array($query)) 
    { 
    $userallowed = htmlspecialchars($row['who_can_see']); 
    } 
    } 

    //$personid is drawn from the database. its the id of the 
    person viewing the link. 

    if ($userallowed == $personid) { 
echo("allowed"); 
    } else { 
echo("not allowed"); 
    die(); 
    } 

    ?> 

回答

0

我只想補充的$personid到查詢(雖然我對你是如何準確地填寫您的posts表懷疑...):

$sql="SELECT `Who_can_see` from `posts` 
     where `post_id` = '$kit' 
     AND `Who_can_see` = '$personid'"; 

如果結果包含一個排,用戶被允許查看該帖子。

順便說一句,我還建議使用準備語句,以避免任何潛在的SQL注入問題。

編輯:基於這樣的事實,Who_can_see可以包含逗號分隔的項目列表,你可以使用你原來的劇本,只是改變你的搭配,例如使用stripos

if (stripos($userallowed, $personid) !== false) { 
    // $personid is found in $userallowed 
    echo("allowed"); 
} else { 
    echo("not allowed"); 
    die(); 
} 
+0

謝謝。這沒有用。我正在使用轉義字符串,這應該夠了,不是嗎? – ariel 2012-04-26 01:06:07

+0

@ariel是的,這就夠了。這兩個值看起來像什麼('Who_can_see'和'$ personid')完全是一個有效的例子? – jeroen 2012-04-26 01:14:26

+0

who_can_see可能有每個人,只是我或它可以是一個人像99394david或多人喜歡99394david,324234smith,34343jane – ariel 2012-04-26 01:17:27