The data is not updated in my BD

0

I try to update my data by checking several checkboxes and it tells me that the insertion was done but when I see my database has not been updated, here is the extract of my code

    <div class="table-responsive">
    <form action="checkruta.php" method="post" >
   <table class="table table-striped table-hover">
    <tr>
               <th>Folio </th>
      <th>Nombre</th>
      <th>Fecha solicitud</th>
      <th>Solicitud</th>
      <th>Estado</th>
      <th>Fecha Reporte</th>
      <th>Seleccionar Ruta</th>
    </tr>
    <?php
    if($filter){
      $sql = mysqli_query($con, "SELECT * FROM contribuyente WHERE estado='$filter'  ORDER BY id ASC");
    }else{
      $sql = mysqli_query($con, "SELECT * FROM contribuyente ORDER BY id ASC");
    }
    if(mysqli_num_rows($sql) == 0){
      echo '<tr><td colspan="8">No hay datos.</td></tr>';
    }else{
      $no = 1;
      while($row = mysqli_fetch_assoc($sql)){
        echo '
        <tr>
          <td>'.$row['id'].'</td>
          <td><a href="profile.php?nik='.$row['nombre'].'"><span class="glyphicon glyphicon-user" aria-hidden="true"></span> '.$row['nombre'].'</a></td>
                        <td>'.$row['fecha'].'</td>
                        <td>'.$row['mensaje'].'</td>
                         <td>'.$row['estado'].'</td>
                         <td>'.$row['fecha_rec'].'</td>
        <td> <input type="checkbox" name="id[]" value=<?php echo $row["id"]; ?></td>

          <td>';
          if($row['estado'] == 'Resuelto'){
            echo '<span class="label label-success">Resuelto</span>';
          }
                        else if ($row['estado'] == 'Pendiente' ){
            echo '<span class="label label-info">Pendiente</span>';
          }
                        else if ($row['estado'] == 'Rechazado' ){
            echo '<span class="label label-warning">Rechazado</span>';
          }
        echo '
          </td>


        </tr>
        ';
        $no++;
      }
    }
    ?>
  </table>
  <input type="submit" name="autorizados" value="Autorizar seleccionados" />
   </div>
   </div>
   </div>

and here is my code that updates

  <?php 

  if (isset($_POST['autorizados'])) { 

//Conexión Mysql.
  $db_host="localhost";
  $db_user="root";
  $db_password= "";
  $db_name="bdpagina";
  $db_connection = mysqli_connect($db_host, $db_user, $db_password, $db_name);


  $id = $_POST['id'];  
  $count = count($id);
  $status = 'AUTORIZADA';

   for ($i=0; $i < $count; $i++) {


    mysqli_query($db_connection,"UPDATE contribuyente SET ruta='$status'
    WHERE id='$id[$i]' LIMIT 1");

}

mysqli_close($db_connection);    
echo "Se actualizo correctamente";     
}
?>
    
asked by Daniela 31.05.2018 в 21:45
source

1 answer

0

It tells you that it was updated correctly because echo "Se actualizo correctamente" is always going to print the way you've set your code.

To check if the query has been completed correctly, I recommend storing it in a variable and then checking it with a if .

$result=mysqli_query($db_connection,"UPDATE contribuyente SET ruta='$status'
WHERE id='$id[$i]' LIMIT 1");

if($result){
 $message = "Consulta realizada.";
} else {
 $message = "Se ha producido un error.";
}

mysqli_close($db_connection);    
echo $message;  

If you want to check if the connection to the database has failed you can use:

if ($db_connection->connect_error) {
  die("Error de conexión: " . $db_connection->connect_error);
} 
    
answered by 31.05.2018 в 21:52