How to capture the id corresponding to the selected row from a Datatable?

3

I want to be able to capture the id corresponding to a row in my Server-side processing datatable , in order to delete and update the records, for now I only have the power to delete and it is that I am stuck in an error that when I select a record to delete it, it ends up eliminating the last row and when I check by means of a die(); , it turns out that the id that arrives to the query to eliminate the row, is the id of the last ending and not of the row that I selected.

Let me explain: when you press the delete button in a row x, it always ends up eliminating the last row of the datatable, which could be? Could you help me?

The code to draw my datatable:

$columns = array(
array( 'db' => 'modelo', 'dt' => 0 ),
array( 'db' => 'marca',  'dt' => 1 ),
array( 'db' => 'fecha_marcaje',   'dt' => 2 ),
array( 'db' => 'id_equipo', 'dt' => 3, 
    'formatter' => function ($d, $row){

      return '<input type="hidden" name="id_equipo" value="' . $d . '"
              <center><button class="boton" type="button">Actualizar</button>    
              <button class="boton" type="submit">Eliminar</button></center>';

    })
);

The code in my html in the form:

<script>
        // FUNCION PARA GENERAR EL DATATABLE
        $(document).ready(function() {
        $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "Data_tabla_ConsultaEquipos.php"
              } );
              } ); //FIN FUNCION

      //FUNCION PARA PREGUNTAR SI QUIERE ELIMINAR EL EQUIPO DESDE EL DATATABLE        
   function ConfirmarDelete()
          {
   var x = confirm("¿Seguro que quieres eliminar este equipo?");
      if (x)
   return true;
      else
   return false;
   } //FIN FUNCION
    </script>

</head>
<body>
    <br><br><br>
    <center><legend class="texto"><h1>CONSULTA / ACTUALIZACIÓN DE EQUIPOS REGISTRADOS</h1></legend></center>
    <br><br><br><br><br>
    <center><div style="width: 70%" align="center">
            <form onsubmit="return ConfirmarDelete()" action="eliminaEquipo.php" method="POST">
            <table id="example" class="display" cellspacing="0">
    <thead>
        <tr>
            <th class="texto">Modelo</th>
            <th class="texto">Marca</th>
            <th class="texto">Fecha Marcaje</th>
            <th class="texto">Acciones</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th class="texto">Modelo</th>
            <th class="texto">Marca</th>
            <th class="texto">Fecha Marcaje</th>
            <th class="texto">Acciones</th>
        </tr>
    </tfoot>
            </table></form></div></center>

The code where I have the query to delete a record, where always comes the id corresponding to the last record shown in datatable , and what I want is that the id corresponds to the row that truly select.

<?php $db_host="localhost"; $db_user="root"; $db_password="";
$db_connection = mysql_connect($db_host, $db_user, $db_password);

if (!$db_connection) {
die('No se ha podido conectar a la base de datos');}

$id_equipo = $_POST['id_equipo'];
print_r($id_equipo); // POR AQUI PUDE NOTAR QUE SIEMPRE ME LLEGA EL ID DE LA ULTIMA FILA DEL DATATABLE.

mysql_select_db("DB", $db_connection);
mysql_query("DELETE FROM equipos WHERE id_equipo = $id_equipo")
or die(mysql_error()); 

header("Location: ConsultarEquipos.php");?>
    
asked by ArtEze 01.06.2017 в 15:52
source

1 answer

2

Ok, I have solved the problem, what I had to do was to give the id value to the submit button, and it already grabs the id corresponding to the selected row to be deleted. thus leaving the code:

$columns = array(
array( 'db' => 'modelo', 'dt' => 0 ),
array( 'db' => 'marca',  'dt' => 1 ),
array( 'db' => 'fecha_marcaje',   'dt' => 2 ),
array( 'db' => 'id_equipo', 'dt' => 3, 
    'formatter' => function ($d, $row){

      return '<center><button class="boton" type="button" id="ModalConsultaEquipos">Actualizar</button>    
              <button class="boton" name="id_equipo" type="submit" value="' . $d . '">Eliminar</button></center>';
    }) );
    
answered by 05.06.2017 / 16:20
source