Xtremer360 Posted May 20, 2011 Share Posted May 20, 2011 What I'm trying to do is have it select the answers that are associated to that poll so that I can make a poll form out of this. function getpoll($dbc) { $query = " SELECT polls.ID, polls.question, polls.totalVotes, (SELECT pollAnswers.ID, pollAnswers.answer FROM pollAnswers WHERE pollAnswers.pollID = polls.ID) FROM polls INNER JOIN pollAnswers ON polls.ID = pollAnswers.pollID WHERE polls.statusID = '1' ORDER BY polls.ID DESC LIMIT 1"; $result = mysqli_query($dbc, $query); $numrows = mysqli_num_rows($result); if ($numrows > "0") { } else { print "<p class=none>There are currently no open polls."; } } Quote Link to comment https://forums.phpfreaks.com/topic/236923-selecting-answers-for-my-poll-form/ Share on other sites More sharing options...
Xtremer360 Posted May 20, 2011 Author Share Posted May 20, 2011 Updated code also with the same question as above. function getpoll($dbc) { $query = " SELECT polls.ID as pollID, polls.question, polls.totalVotes, (SELECT pollAnswers.ID, pollAnswers.answer FROM pollAnswers WHERE pollAnswers.pollID = polls.ID) FROM polls INNER JOIN pollAnswers ON polls.ID = pollAnswers.pollID WHERE polls.statusID = '1' ORDER BY polls.ID DESC LIMIT 1"; $result = mysqli_query($dbc, $query); $numrows = mysqli_num_rows($result); if ($numrows > "0") { while ($row = mysqli_fetch_array($result)) { $fieldarray=array('pollID','question','totalVotes'); foreach ($fieldarray as $fieldlabel) { if (isset($row[$fieldlabel])) { $$fieldlabel=$row[$fieldlabel]; $$fieldlabel=cleanquerydata($$fieldlabel); } } print "<form action=efedmanager/processes/poll.php method=post name=poll id=poll>"; print "<fieldset>"; print "<legend>".$question."</legend>"; foreach ($answer as $answer) { print "<label>"; print "<input type=radio name=Poll value=".$answer." id=".$answerID." />".$answer.""; print "</label>"; } print "<input type=hidden name=pollID value=".$pollID." />"; print "<input type=submit name=submit id=submit value=Vote />"; print "</fieldset>"; print "</form>"; } } else { print "<p class=none>There are currently no open polls."; } } Quote Link to comment https://forums.phpfreaks.com/topic/236923-selecting-answers-for-my-poll-form/#findComment-1217886 Share on other sites More sharing options...
Xtremer360 Posted May 20, 2011 Author Share Posted May 20, 2011 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/236923-selecting-answers-for-my-poll-form/#findComment-1218105 Share on other sites More sharing options...
Xtremer360 Posted May 20, 2011 Author Share Posted May 20, 2011 ? Quote Link to comment https://forums.phpfreaks.com/topic/236923-selecting-answers-for-my-poll-form/#findComment-1218226 Share on other sites More sharing options...
Xtremer360 Posted May 21, 2011 Author Share Posted May 21, 2011 I'm hoping I can get some help with this issue. Quote Link to comment https://forums.phpfreaks.com/topic/236923-selecting-answers-for-my-poll-form/#findComment-1218325 Share on other sites More sharing options...
DavidAM Posted May 21, 2011 Share Posted May 21, 2011 This SQL is not valid. You cannot have a subquery in the SELECT phrase that returns multiple rows. SELECT polls.ID as pollID, polls.question, polls.totalVotes, (SELECT pollAnswers.ID, pollAnswers.answer FROM pollAnswers WHERE pollAnswers.pollID = polls.ID) FROM polls INNER JOIN pollAnswers ON polls.ID = pollAnswers.pollID WHERE polls.statusID = '1' ORDER BY polls.ID DESC LIMIT 1 Since you have LIMIT 1, the WHILE loop is useless, you are only going to have one row to process. I think, in this case, you will have to do two queries. The first query will get the Poll info and the second will get the answer list. Something along these lines: Note: I also rearranged the FORM contents a bit, some of the elements were mixed in with the FIELDSET, and should not have been. function getpoll($dbc) { $query = "SELECT polls.ID as pollID, polls.question, polls.totalVotes, FROM polls WHERE polls.statusID = '1' ORDER BY polls.ID DESC LIMIT 1"; $result = mysqli_query($dbc, $query); if ( ($result) and (mysqli_num_rows($result) > 0) ) { $row = mysqli_fetch_array($result); $pollID = $row['pollID']; $question = $row['question']; $totalVotes = $row['totalVotes']; $query = "SELECT pollAnswers.ID, pollAnswers.answer FROM pollAnswers WHERE pollAnswers.pollID = $pollID"; print "<form action=efedmanager/processes/poll.php method=post name=poll id=poll>"; print "<input type=hidden name=pollID value=".$pollID." />"; print "<fieldset>"; print "<legend>".$question."</legend>"; $result = mysqli_query($dbc, $query); if ( ($result) and (mysqli_num_rows($result) > 0) ) { while ($row = mysqli_fetch_array($result)) { print "<label>"; print "<input type=radio name=Poll value=" . $row['answer'] . " id=" . $row['ID'] . " />" . $row['answer']; print "</label>"; } } print "</fieldset>"; print "<input type=submit name=submit id=submit value=Vote />"; print "</form>"; } else { print "<p class=none>There are currently no open polls."; } } This code is not tested and may contain syntax or logic errors . It is provided as a example of an approach that should work for the stated problem. Quote Link to comment https://forums.phpfreaks.com/topic/236923-selecting-answers-for-my-poll-form/#findComment-1218419 Share on other sites More sharing options...
Xtremer360 Posted May 21, 2011 Author Share Posted May 21, 2011 Thank you for responding. The code you suggested worked great but I had to alter a few things because of how I changed my database but this is what I have now and for some reason its not displaying the text of the (poll_answer). Not sure why. function getpoll($dbc) { $query = " SELECT polls.id as poll_id, polls.poll_question, polls.total_votes FROM polls WHERE polls.status_id = '1' ORDER BY polls.id DESC LIMIT 1"; $result = mysqli_query($dbc, $query); if ( ($result) and (mysqli_num_rows($result) > 0) ) { $row = mysqli_fetch_array($result); $poll_id = $row['poll_id']; $poll_question = $row['poll_question']; $total_votes = $row['total_votes']; $query = " SELECT poll_answers.id as answer_id, poll_answers.poll_answer FROM poll_answers WHERE poll_answers.poll_id = '".$poll_id."'"; print "<form action=efedmanager/processes/poll.php method=post name=poll id=poll>"; print "<fieldset>"; print "<legend>".$poll_question."</legend>"; $result = mysqli_query($dbc, $query); $num_rows = mysqli_num_rows($result); if ( ($result) and (mysqli_num_rows($result) > 0) ) { while ($row = mysqli_fetch_array($result)) { print "<label>"; print "<input type=radio name=Poll value=".$poll_answer." id=".$answer_id." />".$poll_answer.""; print "</label>"; } } print "<input type=hidden name=poll_id value=".$poll_id." />"; print "<input type=submit name=submit id=submit value=Vote />"; print "</fieldset>"; print "</form>"; } else { print "<p class=none>There are currently no open polls."; } } Quote Link to comment https://forums.phpfreaks.com/topic/236923-selecting-answers-for-my-poll-form/#findComment-1218482 Share on other sites More sharing options...
DavidAM Posted May 21, 2011 Share Posted May 21, 2011 The data from the query is stored in $row. You have to reference it from there or copy it to another variable: print "<input type=radio name=Poll value=" . $row['poll_answer'] . " id=" . $row['answer_id'] . " />" . $row['poll_answer'] . ""; Quote Link to comment https://forums.phpfreaks.com/topic/236923-selecting-answers-for-my-poll-form/#findComment-1218517 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.