开发者

PHP + mySQL: three tables, edit an entry with multiple checkboxes

开发者 https://www.devze.com 2023-03-22 00:48 出处:网络
I have three tables - questions (question_id, name_of_question) - events (event_id, name_of_event, etc)

I have three tables

- questions (question_id, name_of_question)
- events (event_id, name_of_event, etc)
- event_questions (question_to_event_id, question_id, event_id)

When the user goes to edit an event (i.e. a row in the event table) they are present with a series of questions

- Sample Question 1
- Sample Question 2
- Sample Question 3...and so on

The way I want it is that some of the checkboxes will be checked (from the previous time they made the entry or edited the entry) but other checkboxes will remain unchecked. Either way, all the possible questions should be listed as options.

Right 开发者_高级运维now I just have (incorrectly)

     $query = 'SELECT id, name 
      FROM questions
      INNER JOIN event_questions 
      ON event_questions.question_id = questions.id
      WHERE event_questions.event_id LIKE ' . $event_id;

      $rows = $wpdb -> get_results($query);

     foreach ($rows as $key=>$value) {

    $question_id = $value->id;
    $question_str = $value->name;?>
    <input type='checkbox' checked='yes' value='<?php echo $question_id; ?>' 
name='question_ids[]' />&nbsp;&nbsp;<?php echo $question_str; ?><br /><?php
}

However, what this does is ONLY show checked checkboxes (and not all possible checkboxes) AND I am passing values into the table more than once (so that an event with questions #1 and #2 becomes questions #1, #2, and #2) which is not what I want at all.

Is there a way to show all checkboxes, with the right checkboxes checked (matching the events up with the correctly selected questions) AND have it update properly?


Select from the questions table, LEFT OUTER JOIN to the event_questions table. This will give you all the questions, and NULL values from the event_questions table where that question is not associated with the event. Those are the unchecked ones.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号