2015-06-30 50 views
-2

我遇到問題從我的查詢結果中獲取有用的信息。我想查詢一個postID的數據庫,如果它與當前帖子相同,我想然後留言,如果不相同,我想顯示一個輸入。這是因爲用戶不能輸入多個帖子,所以一旦用戶輸入了一個帖子,輸入框就會消失,並且提到的消息會顯示「已輸入」。這裏是我的代碼:如果在查詢結果上發表聲明,請使用PHP

$wpdbtest_maindb = new mysqli('localhost', 'username', 'password', 'dbname'); 

$result = mysqli_query($wpdbtest_maindb, "SELECT user_id, post_id 
              FROM tablename 
         WHERE user_id= $user_ID"); 

if (mysqli_num_rows($result) > 0) { 
    while ($row = mysqli_fetch_assoc($result)) { 
     $post_id = $row['post_id']; 
    } 
    if ($id == $post_id) { 
     echo "Already Entered"; 
    } else { 
?> 
<form action="../../mypage.php" method="post">         
        <input type="hidden" name="postid" value="<?= $id ?>" /> 
        <input type="hidden" name="userid" value="<?= $user_ID ?>" /> 
        <p align="right"><input type="submit" value="<?= $user_ID ?>-<?= $id ?> Enter" class="submit" />  </p> 
        </form> 
<?php 


    } 
}} 


?> 
+6

感謝您的數據庫憑據......什麼是你的IP所以他們可以被濫用?而這個代碼究竟是如何工作的? –

+0

'$ post_id = $ row [post_id]'是一個錯誤。大概你的意思是這樣嗎?:'$ post_id = $ row ['post_id']' – David

+0

某些代碼格式會很好*。 –

回答

-1

如果要判斷目前的職位已經在數據庫中使用附加條件

SELECT 
    user_id, 
    post_id 
FROM giveaways 
WHERE 
    user_id = '$user_ID' 
    AND post_id = '$id' 

然後

if (mysqli_num_rows($result) > 0) 
{ 
    echo "Already Entered"; 
} 
else 
{ 
    // print form 
} 
+0

謝謝,這有幫助 –

+0

這正是我想要的。再次感謝 –

+0

好的)),我喜歡幫忙 –