title: Trying to insert into a database as well as post items from a multi select drop-down menu
body: <p>I need to be able to save selected items from a multi select drop-down list into a MySQL DB and display all the selected items. When I hit submit only the last item is saved.</p>
  
  <p>Code originally was for a single item to be selected from a drop-down menu. I have modified it for selecting multiple items (for ease of editing by other people). I have tried various solutions including if is_array, for loops and foreach loops without any luck. Can anyone point me in the right direction please.</p>
  
  <p>HTML code</p>
  
  <pre><code>&lt;select name="topic_id[]" multiple="multiple" id="select"&gt;
      &lt;?php
       $topic_set = find_all_topics();
      while($topic = mysqli_fetch_assoc($topic_set)) {
          foreach($topic_set as $topic) {
              echo "&lt;option value=\"" . $topic['id'] . "\"";
              if($page['topic_id'] == $topic['id']) {
                  echo " selected";
              }
              echo "&gt;" . $topic['menu_name'] . "&lt;/option&gt;";
          }
      }
      mysqli_free_result();
      ?&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>PHP </p>
  
  <pre><code>function insert_page($page) {
          global $db;
          $errors = validate_page($page);
          if(!empty($errors)) {
              return $errors;
          }
          shift_page_positions(0, $page['position'], $page['topic_id']);
          $post_t_ids = array();
          foreach($_POST['topic_id'] as $post_t_id) {
              $post_t_ids[] = (int) $post_t_id;
          }
          $post_t_id_joined = join('), (', $post_t_ids);
          $sql = "INSERT INTO pages ";
          $sql .= "(topic_id, content) ";
          $sql .= "VALUES (";
          $sql .= "'" . db_escape($db, $post_t_id_joined) . "',";
          $sql .= "'" . db_escape($db, $page['content']) . "'";
          $sql .= ")";
          $result = mysqli_query($db, $sql);
          if($result) {
              return true;
          } else {
              echo mysqli_error($db);
              db_disconnect($db);
              exit;
          }
      }
      if(is_post_request()) {
          $page = [];
          $page['topic_id'] = $_POST['topic_id'] ?? '';
          $page['content'] = $_POST['content'] ?? '';
          $result = insert_page($page);
          if(!isset($_POST['topic_id'])) {
              $_POST['topic_id'] = [];
          }
          if($result === true) {
              $new_id = mysqli_insert_id($db);
          } else {
              $errors = $result;
          }
      }
  </code></pre>
  
  <p>The result should be selecting multiple items from the drop-down list and upon clicking submit all selected items should save to the DB and displayed to a different page. No error messages pop up but only the last item selected gets saved and displayed.</p>