Jump to content

Recommended Posts

$questions = mysql_query('SELECT SQL_CALC_FOUND_ROWS * FROM poll_questions WHERE ID_poll = "'.$_SESSION['poll_id'].'" ORDER BY position ASC LIMIT '.$start.', '.$limit.'');

 

How can i prevent questions which are already answered by the same person from being retrieved in this query?

 

 

Link to comment
https://forums.phpfreaks.com/topic/267317-help-with-query-that-prevents/
Share on other sites

Apologizes,

 

I have 4 tables concerning polls:

 

polls, poll_questions, poll_options, poll_answers.

 

in survey.php i want to show each survey, with the belonging questions and belonging options to the questions.

 

the tables:

 

table: Poll_questions

 

ID    ID_poll    poll_question    question_type    positon

 

table: poll_answers

 

ID  ID_option  ID_question  ID_poll  visitor_ip  respondent_id  option_label_case_schaal 

 

table: poll_options

 

ID  ID_question  ID_poll  poll_option  schaallabel_links  schaallabel_rechts  position 

 

table: polls

 

ID    title

 

 

I came with a sql instruction like:

 

AND NOT EXISTS(SELECT poll_answers.ID FROM poll_answers WHERE poll_answers.ID_question = poll_questions.ID AND poll_answers.visitor_ip = '".mysql_real_escape_string(getIP())."') OR poll_answers.respondent_id = '".$_SESSION['resp_id].'").

 

it seems to work, in that questions which are already answered by that particular user, don't show anymore, but it seems that questions are also skipped,

 

say start with question 2, then jump to 4, to 6, to 8.  I don't have this problem with leaving out the AND NOT EXISTS SQL. then the questions show up nicely from question 1 to the last question. So i think there is still something wrong with the sql instruction?

I think, the sql logic in that case could be this :

$query = "SELECT `q`.`poll_question` FROM `poll_questions` as `q` WHERE `q`.`ID_poll` NOT IN (SELECT `a`.`poll_answers.ID` FROM `poll_answers` AS `a` WHERE
`a`.`respondent_id` = 2)";

 

Tip: The integer 2 comming from $_SESSION['resp_id']. Give back the result, please. 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.