2014-11-14 50 views
0

我一直在試圖創建一個輪詢系統,對數據庫運行查詢,如果它發現匹配,不包括該答案。PHP/MySQL的 - 停止Foreach運行相同的查詢

這是一個輪詢系統,當用戶投票投票時,它將pollid和userid存儲在數據庫表中,然後在重新訪問該頁面時檢索它,以便他們之前投票的投票沒有顯示。

我會盡我所能解釋,我發現很難理解我自己。

在我的Pollsdone(其中poll和完成調查的用戶名)表中,它包含2行和2列。

的兩行是:
pollid
的userid

兩列pollsdone是(用戶標識無所謂):
26,USERID
76,USERID

SQL:

$sql = "SELECT * FROM Polls"; 
$completedVoteSearch = "SELECT * FROM Pollsdone WHERE userid='10'"; 

W我使用的母雞:

foreach ($db->query($sql) as $row) { 
    foreach ($db->query($completedVoteSearch) as $done) { 
     if ($done['pollid'] != $row['pollid']) { 
     // CREATE POLL CODE 

它將對數據庫運行查詢並正確選擇哪些匹配。問題是,它也會選擇類似「44,76」,並顯示投票「76」,即使它與「76,76」匹配
希望這張照片可以幫助你更好地理解它:(不是它說「我們有一場比賽!」並顯示投票仍然)

http://i.stack.imgur.com/NfTLS.png

它仍然顯示即使它應該因爲其他查詢的隱藏的民意調查,我怎麼去阻止呢? 任何幫助將不勝感激!

下面的代碼即時通訊使用(僅供參考,有沒有錯誤,它並不需要進行「調試」)

try { 
          $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_db", $mysql_user, $mysql_pass); 
          $sql = "SELECT * FROM Polls"; 
          $completedVoteSearch = "SELECT * FROM Pollsdone WHERE userid='10'"; 

          foreach ($db->query($sql) as $row) { 
           foreach ($db->query($completedVoteSearch) as $done) { 
            if ($done['pollid'] == $row['pollid']) { 
             echo "We have a match!"; 
            } 
            if ($done['pollid'] != $row['pollid']) { 
             echo "<br/>"; 
             echo $done['pollid'] . ", "; 
             echo $row['pollid'] . ", "; 
             echo "<br/>"; 
             $pollid = $row['pollid']; 
             $title = $row['title']; 
             $type = $row['type']; 
              if ($type == "s") { 
               $type = "radio"; 
              } else { 
               $type = "checkbox"; 
              } 

             $option1 = $row['option1']; 
             $option2 = $row['option2']; 
             $option3 = $row['option3']; 
             $option4 = $row['option4']; 
             $option5 = $row['option5']; 
             $option6 = $row['option6']; 

             $option1vote = $row['option1vote']; 
             $option2vote = $row['option2vote']; 
             $option3vote = $row['option3vote']; 
             $option4vote = $row['option4vote']; 
             $option5vote = $row['option5vote']; 
             $option6vote = $row['option6vote']; 

             $option1voteColumn = "option1vote"; 
             $option2voteColumn = "option2vote"; 
             $option3voteColumn = "option3vote"; 
             $option4voteColumn = "option4vote"; 
             $option5voteColumn = "option5vote"; 
             $option6voteColumn = "option6vote"; 
            } 
           } 
          // $isCompleteSQL = "SELECT * FROM Pollsdone WHERE userid='10' "; 
          // if() 

          if (strlen($option3) < 1 && strlen($option2) > 0) { 
           echo '<div class="userPoll"> 
            <div class="pollTitle"> 
             <h3 class="pollHeader">' . $title . ' (ID = ' . $pollid . ')</h3> 
            </div> 
            <div class="pollContent"> 
             <p class="pollSubTitle">Created By <a href="profile.php">Username</a></p> 
             <form class="poll" method="POST" action="mypolls.php"> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'" value="option1vote"><p class="pollAnswerText">' . $option1 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'" value="option2vote"><p class="pollAnswerText">' . $option2 . '</p></label><br/> 
              <input type="submit" class="pollSubmit btn btn-default"/> 
             </form> 
            </div> 
           </div>'; 

           if ($_POST[$pollid] . $pollid == $option1voteColumn . $pollid) { 
            $option1vote = ($option1vote + 1);       
            $voteSQL = "UPDATE Polls SET ".$option1voteColumn."='".$option1vote."' WHERE pollid='".$pollid."'"; 
            $db->exec($voteSQL); 
           } 
          } 

          if (strlen($option4) < 1 && strlen($option3) > 0) { 
           echo '<div class="userPoll"> 
            <div class="pollTitle"> 
             <h3 class="pollHeader">' . $title . ' (ID = ' . $pollid . ')</h3> 
            </div> 
            <div class="pollContent"> 
             <p class="pollSubTitle">Created By <a href="profile.php">Username</a></p> 
             <form class="poll" method="POST" action="mypolls.php"> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option1 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option2 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option3 . '</p></label><br/> 
              <input type="submit" class="pollSubmit btn btn-default"/> 
             </form> 
            </div> 
           </div>'; 
          } 

          if (strlen($option5) < 1 && strlen($option4) > 0) { 
           echo '<div class="userPoll"> 
            <div class="pollTitle"> 
             <h3 class="pollHeader">' . $title . ' (ID = ' . $pollid . ')</h3> 
            </div> 
            <div class="pollContent"> 
             <p class="pollSubTitle">Created By <a href="profile.php">Username</a></p> 
             <form class="poll" method="POST" action="mypolls.php"> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option1 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option2 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option3 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option4 . '</p></label><br/> 
              <input type="submit" class="pollSubmit btn btn-default"/> 
             </form> 
            </div> 
           </div>'; 
          } 

          if (strlen($option6) < 1 && strlen($option5) > 0) { 
           echo '<div class="userPoll"> 
            <div class="pollTitle"> 
             <h3 class="pollHeader">' . $title . ' (ID = ' . $pollid . ')</h3> 
            </div> 
            <div class="pollContent"> 
             <p class="pollSubTitle">Created By <a href="profile.php">Username</a></p> 
             <form class="poll" method="POST" action="mypolls.php"> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option1 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option2 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option3 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option4 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option5 . '</p></label><br/> 
              <input type="submit" class="pollSubmit btn btn-default"/> 
             </form> 
            </div> 
           </div>'; 
          } 

          if (strlen($option6) > 0) { 
           echo '<div class="userPoll"> 
            <div class="pollTitle"> 
             <h3 class="pollHeader">' . $title . ' (ID = ' . $pollid . ')</h3> 
            </div> 
            <div class="pollContent"> 
             <p class="pollSubTitle">Created By <a href="profile.php">Username</a></p> 
             <form class="poll" method="POST" action="mypolls.php"> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option1 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option2 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option3 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option4 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option5 . '</p></label><br/> 
              <label><input type="' . $type . '" class="pollAnswer" name="'. $pollid .'"><p class="pollAnswerText">' . $option6 . '</p></label><br/> 
              <input type="submit" class="pollSubmit btn btn-default"/> 
             </form> 
            </div> 
           </div>'; 
          } 
         }$db = null; 

         } catch(PDOException $e) { 
          $e->getMessage(); 
         } 
+1

什麼是'44,76',什麼是'76,76'?你試圖完成的主要目標是什麼?不顯示用戶已經參與的民意調查? 'select * from polls where pollid NOT IN(select polidone where user_id = 10)' – 2014-11-14 20:13:34

+0

這可能有效。我會嘗試並在@U_mulder之前給它! – 2014-11-14 20:39:03

+0

然後我得到錯誤「Warning:foreach()在第70行中的[..loc ..]提供的無效參數」foreach是:'foreach($ db-> query($ sql)as $ row) '與SQL是:'$ sql =「選擇*從民意調查在哪裏pollid不在(選擇pollid從pollsdone WHERE userid = 10)」;' – 2014-11-14 20:51:02

回答

0

所以,如果我得到你的權利,你想選擇用戶尚未參與的民意調查。 您可以使用此查詢:

select * from `polls_table` 
where `pollid` NOT IN 
    (select `pollid` from `pollsdone` 
    where `user_id` = 10)