Jump to content
Fórum Script Brasil
  • 0

Update múltiplos registros com checkbox


Question

Estou tentando fazer um update de vários registros com checkbox, mais não está atualizando no banco de dados MySQL. Segue o meu código, desde já agradeço!

<?php 
include_once("../config/conexao.php");
$nivel = $_SESSION['nivel'];
   
if ($_SESSION['nivel'] == 4)
    {
$sql = "SELECT ID, NOME, VOLUNTARIO FROM cidadao ORDER BY NOME";
$res = mysql_query($sql) or die (mysql_error() );

if (mysql_num_rows($res) > 0 )
echo '<form method="post">';
?>
<table width="431" border="0" align="center" cellpadding="0" class="bordaFina">
    <tr bgcolor="#999999" >
      <td width="302" align="center" valign="middle" bgcolor="#F0F0F0" class="style27">Nome  </td>
      <td width="123" align="center" valign="middle" bgcolor="#F0F0F0" class="style27">Voluntário</td>
    </tr>
    <?php
while ($row = mysql_fetch_assoc($res))
    {     
    ?>
    <tr >
      <td><?php echo $row["NOME"];?></div></td>
  <td ><div align="center"> 
 <input type="checkbox" name= "VOLUNTARIOS[<?php  $row["ID"] ?>]" value="1" <?php if($row["VOLUNTARIO"] == 1){ echo "checked";} ?>> 
 <input type="hidden" name= "VOLUNTARIOS[<?php $row["ID"] ?>]" value="0" <?php if($row["VOLUNTARIO"] == 0){ echo ""; }  ?>>
  </div></td>
  </tr>
    <?php 
  }  //End While
  ?>
</table>
  
<p><div align="center"><?php echo '<input type="submit"name= "submit" value= "Salvar">';?></div>
  <?php

if (isset ($_POST["submit"]))
{        
        foreach($_POST['VOLUNTARIOS'] as $ID => $VOL) 
          {mysql_query("update cidadao SET VOLUNTARIO ='$VOL' WHERE ID='$ID'")or die(mysql_error()); }                    
           echo "<meta HTTP-EQUIV='refresh' CONTENT='1;URL=teste.php'>";
}
?>
<?php
echo'<br>';
echo '</form>';
}//endif
?>

form.PNG

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Tenta usar isso, se não der certo vou tentar te ajudar.

 

<?php 
 include_once("../config/conexao.php");
 $nivel = $_SESSION['nivel'];
    
 if ($_SESSION['nivel'] == 4){
   $sql = "SELECT ID, NOME, VOLUNTARIO FROM cidadao ORDER BY NOME";
   $res = mysql_query($sql) or die (mysql_error()); 
   if (mysql_num_rows($res) > 0 ){
 ?>
 <form method="post">
   <table width="431" border="0" align="center" cellpadding="0" class="bordaFina">
     <tr bgcolor="#999999" >
       <td width="302" align="center" valign="middle" bgcolor="#F0F0F0" class="style27">Nome  </td>
       <td width="123" align="center" valign="middle" bgcolor="#F0F0F0" class="style27">Voluntário</td>
     </tr>
     <?php
      while ($row = mysql_fetch_assoc($res)){     
     ?>
     <tr >
       <td><?php echo $row["NOME"];?></div></td>
       <td >
         <div align="center"> 
          <input type="checkbox" name= "VOLUNTARIOS[<?php  $row["ID"] ?>]" value="1" <?php if($row["VOLUNTARIO"] == 1){ echo "checked";} ?>> 
          <input type="hidden" name= "ID[]" value="<?php if($row["VOLUNTARIO"] == 0) echo ""; else: echo $row["ID"]; endif;  ?>" >
         </div>
      </td>
   </tr>
    <?php 
   }  //End While
   ?>
 </table>
   
 <p><div align="center"><?php echo '<input type="submit"name= "submit" value= "Salvar">';?></div>
 </br>
 </form>
 <?php 
}
}
  ?>
   <?php
    $dados = filter_input_array(INPUT_POST,FILTER_DEFAULT);
    if (isset($dados['submit'])):      
         foreach($dados['id'] as $ID) {
            $VOL =   $dados['VOLUNTARIO'][$ID];
           mysql_query("update cidadao SET VOLUNTARIO ='$VOL' WHERE ID='$ID'")or die(mysql_error()); }                    
            echo "<meta HTTP-EQUIV='refresh' CONTENT='1;URL=teste.php'>";
          }
    endif;
    ?>

Mas é preciso melhorar um pouco esses códigos, com força de coragem chega lá, já passei por isso.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Cloud Computing


  • Forum Statistics

    • Total Topics
      148911
    • Total Posts
      645018
×
×
  • Create New...