Help with getting the id of a PHP / MYSQL form

0

Good I have a problem to obtain the id of a form:

I have in my database a table called assistances which I want to edit an observation by form whenever I want.

in my list I have:

<html>
 <head>
 <body>
<?php 
  include 'conexion.php';

  $datos_asistencia = $conexion->query("SELECT * FROM asistencias");
  //$datos_trabajador = $conexion->query("SELECT * FROM empleados");//


?>

<!--parte central de asistencias donde se imprimen los empleados a los cuales se les puede agregar una asistencia solo los activos-->
<small><h3>Lista de asistencia (completas)</h3></small>
<hr>

<table class="table table-bordered">
  <thead>
    <th>ID</th>
    <th>Fecha</th>
    <th>Hora Entrada</th>
    <th>Hora Salida</th>
    <th>Observación</th>
    <th>Añadir</th>
  </thead>
  <?php while ($asistencias = mysqli_fetch_array($datos_asistencia)) { ?>
  <tbody>
    <tr>
      <td><?php echo $asistencias["id"]; ?></td>
      <td><?php echo $asistencias["fecha"]; ?></td>
      <td><?php echo $asistencias["horaEntrada"]; ?></td>
      <td><?php echo $asistencias["horaSalida"]; ?></td>
      <td><?php echo $asistencias["observacion"]; ?></td>
      <td><a href="crear-observacion.php?id=<?php echo $asistencias["id"]; ?>">Añadir</a></td>
    </tr>

     <?php  } ?>
  </tbody>
</table>

in assists I have:

asistencias (id, fecha, horaEntrada, horaSalida, observacion, empleado_id)

And I want to edit observation.

I have my form where "supposedly I get the id":

<form method="post" action="guardar-observacion.php">
<input type="hidden" value="<?php echo $asistencia["id"]; ?>">
<textarea name="observacion" value="<?php echo $asistencia['observacion']; ?
>" cols="30" rows="5" class="form-control"></textarea> <br>
<button class="btn btn-primary">Agregar</button>
</form>

and in my keep-observation.php I have:

<?php 
//UPDATE 'asistencias' SET 'observacion'= "Hola Mundo" WHERE 'id' = 17//
include 'conexion.php';

$observacion = $_POST["observacion"];

$agregar = "UPDATE 'asistencias' SET 'observacion'= '$observacion' WHERE 
'id' = '{$_GET["id"]}' ";
$result = mysqli_query($conexion, $agregar);
?>

Now what I need is to take and edit the selected id of the form, and save it already updated.

    
asked by Luemapo 10.07.2017 в 22:46
source

2 answers

2

Add name to the input hidden:

<form method="post" action="guardar-observacion.php">
    <input type="hidden" name="id" value="<?php echo $asistencia["id"]; ?>">
    <textarea name="observacion" value="<?php echo $asistencia['observacion']; ?
    >" cols="30" rows="5" class="form-control"></textarea> <br>
    <button class="btn btn-primary">Agregar</button>
    </form>

And in keep-observation.php collects the variables by post or request:

 <?php 
    ....

    $agregar = "UPDATE 'asistencias' SET 'observacion'= '$observacion' WHERE 
    'id' = '{$_POST["id"]}' ";
    ....
    ?>
    
answered by 10.07.2017 в 23:05
0

Fixed: The problem was in the query.

$id = $_POST["id"];
$observacion = $_POST["observacion"];
$agregar = "UPDATE 'asistencias' SET 'observacion'= '$observacion' WHERE 
'id' = '$id' ";
$result = mysqli_query($conexion, $agregar);
    
answered by 11.07.2017 в 04:39