title: Insert value of checkbox and number input into database
body: <p>I tried combining two answers from here:
  <a href="https://stackoverflow.com/questions/18572351/counting-how-many-checkbox-are-checked-php-html">counting how many checkbox are checked php/html</a>
  And here:
  <a href="https://stackoverflow.com/questions/18574626/validating-input-types-checkbox-and-number-php-html">validating input types &quot;checkbox&quot; and &quot;number&quot; php/html</a></p>
  
  <p>My goal is to count how many checkboxes were selected, and insert their values along with the number value in the number input.</p>
  
  <p>I found various other posts about it, but couldn't figure out an answer.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>echo '&lt;form action="" method="post"&gt;';
  </code></pre>
  
  <p>Then I have a for loop, where I create my table rows, with the checkboxes.
  Where $id = $row[0] is updated on each loop, which is the id equivalent of my entry.
  (The output of </p>
  
  <pre><code>echo '&lt;td style="vertical-align: top;"&gt;' . $row[0] . '&lt;/td&gt;';
  </code></pre>
  
  <p>Shows the id correctly)</p>
  
  <pre><code>echo '&lt;tr&gt;';
  echo "&lt;td style='vertical-align: top;'&gt;&lt;input type='checkbox' name='choice[$id][id]' value='$id'&gt;&lt;/td&gt;";
  echo "&lt;td style='vertical-align: top;'&gt;&lt;input type='number' name='choice[$id][order]' size='20'&gt;&lt;/td&gt;";
  echo '&lt;/tr&gt;';
  echo '&lt;b&gt; &lt;input type="submit" value="Insert"&gt;&lt;/b&gt;';
  </code></pre>
  
  <p>Then, on the next page, I have this:</p>
  
  <pre><code>$var_checkbox=$_POST['choice'];
  $sql_var_id = "SELECT id FROM custom_form WHERE id=(SELECT max(id) FROM custom_form)";
  $var_id_result = mysqli_query($link,$sql_var_id);
  $var_id = mysqli_fetch_array($var_id_result);
  
          $count=count($var_checkbox[$var_id[0]]);
  
          for($i=0; $i&lt;$count; $i++){
              if($var_checkbox[$i]!= NULL){
  
                  $sql1 = sprintf("INSERT INTO custom_form_has_property (custom_form_id,property_id,field_order) VALUES ('%d','%d','%d');",
                      $var_id[0],
                      $var_checkbox[$var_id[0]][id],
                      $var_checkbox[$var_id[0]][order]
                  );
                  $result1 = mysqli_query($link,$sql1);
              }
          }
  </code></pre>
  
  <p>The problem is, that no values of checkboxes or numbers are inserted.</p>
  
  <p>(As a side note, to try and be more specific)
  If instead of matrixes, I use </p>
  
  <pre><code>   echo "&lt;td style='vertical-align: top;'&gt;&lt;input type='checkbox' name='choice[]' value='$row[0]'&gt;&lt;/td&gt;";
     echo '&lt;td style="vertical-align: top;"&gt;&lt;input type="number" name="order[]" size="2"&gt;&lt;/td&gt;';
  </code></pre>
  
  <p>And </p>
  
  <pre><code>    $var_checkbox=$_POST['choice'];
      $order = $_POST['order'];
  </code></pre>
  
  <p>Then</p>
  
  <pre><code>echo "Orders: $order[0],$order[1],$order[2],$order[3],$order[4],$order[5],$ordemr[6]";
  echo "Choices: $var_checkbox[0],$var_checkbox[1],$var_checkbox[2],$var_checkbox[3],$var_checkbox[4],$var_checkbox[5],$var_checkbox[6]";
  </code></pre>
  
  <p>Will output </p>
  
  <blockquote>
    <p>Orders: 1,,2,,3,,Choices: 1,3,13,,,,</p>
  </blockquote>
  
  <p>I need the choiced from the checkboxes to be somehow linked to the orders from the number field. And the only way to achieve that, is if they have the same name, but different indexes, which is why I have the matrix, in the first index, it's saved the id number, which is generated on each loop, and in the second, the actual name, which makes the distinction between them (id and order).</p>
  
  <p>I tried various other things, but it all comes back to the same problem...
  The value of order (number) is stored into the array, even when null, while the value of choices (checkbox) doesnt.
  I could do an array_filter(), but there is no guarantee that the user will not input an order, while not ticking a choice checkbox.
  If I would compare the length of choice with the length of order, after filtered, and if they output the same size, there is still no guarantee, that the user didnt check checkbox of line x, and added a value in the order field, on line y.</p>
  
  <p>Maybe there is a way to pass unchecked values to the choice[] variable as null, the same way it happens in order[]?</p>