title: How to insert database rows if there are no answers
body: <p>I am trying to insert each answer for each question in the database. Problem is that there is a possibility that a question may have no answers, so I tried the code below but it does not insert a db row if a question has no answer, what I was trying to do is that if no answer then display the string <code>No Answer</code> under the <code>Answer</code> column for that question:</p>
  
  <pre><code> $answersql = "INSERT INTO Answer (QuestionId, Answer) 
      VALUES (?, ?)";
  if (!$insertanswer = $mysqli-&gt;prepare($answersql)) {
      // Handle errors with prepare operation here
      echo __LINE__.': '.$mysqli-&gt;error;
  }
  
  if( $insert &amp;&amp; $insertanswer)
  {
  
      $c = count($_POST['numQuestion']);
      $question_ids = array();
  
      for($i = 0;  $i &lt; $c; $i++ )
      {
  
  ... //Question INSERT goes here
  
          $questionId = $mysqli-&gt;insert_id;
  
              $question_ids[$questionNo] = $questionId;
  
  }
  
          $results = $_POST['value'];
          foreach($results as $id =&gt; $value) 
          {
              $answer = $value;
  
              $quesid = (int)$question_ids[$id];   
  
              foreach($value as $answer) 
              {
  
              if($answer == '' || $answer === null){
                  $answer = 'No Answer';
              }
  
                  $insertanswer-&gt;bind_param("is", $quesid, $answer);
  
                  $insertanswer-&gt;execute();
  
                  if ($insertanswer-&gt;errno) {
                      // Handle query error here
                      echo __LINE__.': '.$insertanswer-&gt;error;
                      break 7;
                  }
              }
          }
  
  
      //close your statements at the end
  
  
      $insertanswer-&gt;close();
  
  }
  </code></pre>
  
  <p>The <code>['value']</code> comes from an input:</p>
  
  <pre><code>var $newBtn = $(("&lt;input class='answerBtnsRow answers' type='button' style='display:%s;' onclick='btnclick(this, " + gQuestionIndex + ");' /&gt;").replace('%s', $this.is(':visible') ? 'inline-block' : 'none')).attr('name', "value[" + gQuestionIndex + "][]").attr('value', $this.val()).attr('class', $this.attr('class')).attr('id', $this.attr('id') + 'Row');
  </code></pre>
  
  <p>Below is the SHOW CREATE TABLE for the <code>Answer</code> Table:</p>
  
  <pre><code>CREATE TABLE `Answer` (
   `AnswerId` int(10) NOT NULL AUTO_INCREMENT,
   `QuestionId` int(10) NOT NULL,
   `Answer` varchar(10) DEFAULT NULL,
   PRIMARY KEY (`AnswerId`)
  ) ENGINE=InnoDB AUTO_INCREMENT=280 DEFAULT CHARSET=utf8
  </code></pre>
  
  <p>Below is a var dump where if I set question 1 to have answers <code>B</code> and <code>C</code>, question 2 to have no answers, question 3 to have answer <code>B</code> and question 4 to have no answer, it outputs the following from var dump:</p>
  
  <pre><code>var_dump($question_ids);
      var_dump($results);
  
  
  array(4) { 
  [1]=&gt; int(265) 
  [2]=&gt; int(266) 
  [3]=&gt; int(267) 
  [4]=&gt; int(268) 
  } 
  [1]=&gt; array(2) { 
  [0]=&gt; string(1) "B" 
  [1]=&gt; string(1) "C" 
  } 
  [3]=&gt; array(1) { 
  [0]=&gt; string(1) "B" 
  } 
  </code></pre>
  
  <p>So it outputs those set of answers in questions 1 and 3. But does not post <code>No Answer</code> for question 2 and 4.</p>
  
  <p>If all questions had an answer as thisL</p>
  
  <pre><code>question 1: B C
  question 2: A
  question 3: B
  question 4: A C
  </code></pre>
  
  <p>Then the var dump displays this in var dump:</p>
  
  <pre><code>array(4) { 
  [1]=&gt; int(277) 
  [2]=&gt; int(278) 
  [3]=&gt; int(279) 
  [4]=&gt; int(280) 
  } 
  array(4) { 
  [1]=&gt; array(2) { 
  [0]=&gt; string(1) "B" 
  [1]=&gt; string(1) "C" } 
  [2]=&gt; array(1) { 
  [0]=&gt; string(1) "A" 
  } 
  [3]=&gt; array(1) 
  { 
  [0]=&gt; string(1) "B" 
  } 
  [4]=&gt; array(2) 
  { 
  [0]=&gt; string(1) "A" 
  [1]=&gt; string(1) "C" 
  } }} 
  </code></pre>
  
  <p>As you can see for each question is inserts the relevant answers. So the issu is if the question has no answers that it does not insert a db row and state <code>No Answer</code>. My question is how to include a <code>No Answer</code>for each question in the db that has no answers selected in those questions?</p>