title: Checkbox checked or unchecked boolean php mysql
body: <p>I am trying to make this form with checkboxes get values 1 or 0, sort of a boolean, and UPDATE it on mysql, but I havent found a way yet. Maybe you can help. I'll post the form and the code that receives it. I am sure there might be a conflict of loops, but I tryed everything and couldnt get it working. Tks in advance.</p>
  
  <p>THE FORM</p>
  
  <pre><code>&lt;form action="esconde_cat.php" method="post"&gt;
      &lt;table width="300px" align="center" width="140" border="1"&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td width="200"&gt;Categoria&lt;/td&gt;
              &lt;td width="100"&gt;Mostrar&lt;/td&gt;
          &lt;/tr&gt;
          &lt;?php
              include "conecta.php";
              include "verifica.php";
  
              $sql = "SELECT * FROM categorias ORDER BY ordem";
              $res = mysql_query($sql) or die (mysql_error());
              while ($linha=mysql_fetch_array($res)) {
          ?&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;input name="user[]" type="checkbox" value="true"/&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?=$linha['1']; ?&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?php if ($linha['3'] == TRUE){ echo 'SIM'; } else {echo 'NÃO'; } ?&gt;&lt;/td&gt;
              &lt;/td&gt;
          &lt;/tr&gt; 
  
          &lt;?php 
              }
          ?&gt;
      &lt;/table&gt;
      &lt;br/&gt;
      &lt;table align="center"&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;input type="submit" name="Delete" type="button" value="Alterar"&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>THE CODE THAT RECEIVES THE FORM AND UPDATE MYSQL</p>
  
  <pre><code>&lt;?php
  
  include "conecta.php";
  include "verifica.php";
  
  \\THIS PART WORKS, GETS IF THE CHECKBOX IS CHECKED AND RECORD IT ON DB
  
  if(isset($_POST['user'])) {
      foreach ($_POST['user'] as $key =&gt; $read){
          $read =(@$_POST['user']=='0')? $_POST['user']:'1';
          echo $read;
          $sql = "UPDATE categorias SET mostrar='$read' WHERE ordem='$key'";
          $res = mysql_query($sql) or die ("Erro ao excluir") . mysql.error();
      }
  }
  
  \\ON THIS PART OF THE CODE THAT I CANT GET THE VARIABLE TO GET THE
  \\VALUE FROM CHEBOXES THAT ARE UNCHECKED RECORDED TO DATABASE
  
  if (!isset($_POST['user'])) {
      foreach ($_POST['user'] as $chave =&gt; $leia) {
          $leia =(@$_POST['user']=='1')? $_POST['user']:'0';
          echo $leia;
          $sql = "UPDATE categorias SET mostrar='$leia' WHERE ordem='$chave'";
          $res = mysql_query($sql) or die ("Erro ao excluir") . mysql.error();
      }
  }
  
  ?&gt;
  </code></pre>