我一直在尝试创建一个针对数据库运行查询的轮询系统,如果找到匹配项,则不包括该答案。
这是一个投票系统,当用户对投票进行投票时,它会将pollid和userid存储在数据库表中,然后在重新访问页面时检索它,以便之前投票的投票不会显示。
我会尽力解释,我发现自己很难理解。
在我的Pollsdone中(民意调查和完成民意调查的用户ID)表格中包含2行2列。
这两行是:
pollid
用户ID
民意调查的两栏是(Userid并不重要):
26,USERID
76,USERID
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
它将对数据库运行查询并正确选择哪些匹配。问题是,它也会选择“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();
}
答案 0 :(得分:0)
所以,如果我说得对,那你就试图选择用户尚未参与的民意调查。 您可以使用此查询:
select * from `polls_table`
where `pollid` NOT IN
(select `pollid` from `pollsdone`
where `user_id` = 10)