【问题标题】:PHP/MySQL - Stopping Foreach Running The Same QueryPHP/MySQL - 停止运行相同查询的 Foreach
【发布时间】:2015-01-12 07:25:19
【问题描述】:

我一直在尝试创建一个投票系统,该系统对数据库运行查询,如果找到匹配项,则不包含该答案。

这是一个投票系统,当用户对投票进行投票时,它将 pollid 和 userid 存储在数据库表中,然后在他们重新访问页面时检索它,因此不会显示他们之前投票的投票。

我会尽力解释,我发现自己很难理解。

在我的 Pollsdone(投票和完成投票的用户 ID 所在的位置)表中,它包含 2 行和 2 列。

这两行是:
花粉
用户名

pollsdone 的两列是(用户 ID 无关紧要):
26、用户名
76、用户名

SQL:

$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']) {
         // CREATE POLL CODE

它将对数据库运行查询并正确选择匹配的。问题是,即使它与“76,76”匹配,它也会选择像“44,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();
                        }

【问题讨论】:

  • 什么是44,76,什么是76,76?你试图达到的主要目标是什么?不显示用户已经参与的投票? select * from Polls where pollid NOT IN (select pollid from polldone where user_id = 10)
  • 这可能行得通。我会尝试并在之前给它@U_mulder!
  • 然后我收到错误“警告:第 70 行 [..loc..] 中为 foreach() 提供的参数无效” foreach 为:foreach ($db-&gt;query($sql) as $row),SQL 为:@ 987654329@
  • 不,抱歉。我只是错误地复制了您的 SQL,它可以完美运行并且使用的代码更少。创建一个答案,您将成为第一名! :) 非常感谢!

标签: php mysql if-statement foreach


【解决方案1】:

所以,如果我没听错的话,您就是在尝试选择用户尚未参与的投票。 你可以使用这个查询:

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

【讨论】:

    猜你喜欢
    • 2011-04-16
    • 2011-01-05
    • 2016-02-17
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多