title: PHP Posting an array from a form is not working
body: <p>I have some checkboxes with printed values according to the database which are working fine.<br>When I submit the form it gives me an error saying that the variable which I posted from the checkboxes array is not defined and that way I cant insert the values in the database.<a href="https://i.stack.imgur.com/OhRXF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OhRXF.png" alt="This is the error" /></a></p>
  <p>The PHP code to post the array and insert in the database:</p>
  <pre><code>//The posts above don't matter because the first query works just fine
  
  $candCurso     = $_POST['curso'];
  
  $query = &quot;INSERT INTO tbl_candidatos(cand_nome, cand_datanasc, cand_nac, cand_nat, cand_gen, cand_morada, cand_codpost, cand_cidade, cand_email, cand_telefone, cand_telemovel, cand_tipodoc, cand_ndoc, cand_valdoc, cand_nif, cand_hablit, cand_sitprof, cand_grau, cand_instgrau, cand_datagrau, cand_instfunc, datainsc) VALUES ('$candNome','$candDataNasc','$candNac','$candNat','$candGen','$candMorada' ,'$candCodPost','$candCidade','$candEmail','$candTelefone','$candTelemovel','$candTipoDoc','$candNDoc','$candValDoc', '$candNif','$candHabLit','$candSitProf','$candGrau','$candInstGrau', ' $candDataGrau','$candInstFunc', '$dins')&quot;;
  $result = mysqli_query($link, $query); // Executa a instrução MYSQL
  
  if($result){
      echo &quot;&lt;script&gt;alert('I entered the result if');&lt;/script&gt;&quot;;
      $lastID = mysqli_insert_id($link);
          foreach($candCurso as $value) {
              echo &quot;&lt;script&gt;alert('I entered the foreach if');&lt;/script&gt;&quot;;
              $data = date('Y-m-d');
              $sql = &quot;INSERT INTO tbl_professor_curso(cand_id, cur_id, data_admissao) VALUES ($lastID, '$value', '$data')&quot;;
              $rr = mysqli_query($link, $sql); // Executa a instrução
          }
  </code></pre>
  <p>The code doesn't run the foreach statement because of the variable <b>$candCurso</b>.<br>
  The form:</p>
  <pre><code>&lt;!-- Message --&gt;
  &lt;div class=&quot;col-md-12&quot;&gt;
      &lt;!-- Form Group --&gt;
      &lt;div class=&quot;form-group&quot;&gt;
      &lt;label for=&quot;form_message&quot;&gt;Select *&lt;/label&gt;
      &lt;?php
          $cursos = getCursos($link);
          foreach ($cursos as $curso): ?&gt;
          &lt;div class=&quot;checkbox&quot;&gt;
              &lt;label &gt;
                  &lt;input type=&quot;checkbox&quot; class=&quot;form-control&quot; name=&quot;curso[]&quot; value=&quot;&lt;?= $curso['cur_id'] ?&gt;&quot;&gt; &lt;?= $curso['cur_nome'] ?&gt;
              &lt;/label&gt;
          &lt;/div&gt;
      &lt;?php
          endforeach;
      ?&gt;
      &lt;/div&gt;&lt;!-- /End Form Group --&gt;
  &lt;/div&gt;&lt;!-- /End Message --&gt;
  </code></pre>
  <p>Any help is appreciated as I'm looking to fix this for hours.</p>