Ver Mensaje Individual
  #9 (permalink)  
Antiguo 15/04/2021, 08:46
pilucho
 
Fecha de Ingreso: noviembre-2004
Ubicación: NULL
Mensajes: 652
Antigüedad: 19 años, 4 meses
Puntos: 6
Respuesta: agregar un DELETE despues de If else...

Tome el código de VER (detalles) y lo agregue como delete pero solo puedo ver el nombre mas el botón sin ninguna acción
por mas que le de clic para que proceda no hay resultado

Por otro lado este es el código de "insert.php" que esta en el video y código fuente.
le agregue la acción eliminar y ahora Ya no Actualiza solo me permite VER AGREGAR pero NO EDITAR Ni BORRAR

Código PHP:
Ver original
  1. <?php  
  2.  $connect = mysqli_connect("localhost", "root", "", "myDB");  
  3.  if(!empty($_POST))  
  4.  {  
  5.       $output = '';  
  6.       $message = '';  
  7.       $name = mysqli_real_escape_string($connect, $_POST["name"]);
  8.       if($_POST["students_id"] != '')  
  9.       {  
  10.            $query = "UPDATE students SET name='$name' WHERE id='".$_POST["students_id"]."'";  
  11.            $message = 'Data Updated';  
  12.       }  
  13.       else  
  14.       {  
  15.            $query = "INSERT INTO students(name) VALUES('$name')";  
  16.            $message = 'Data Inserted';  
  17.       }
  18.  
  19.       // DELETE
  20.       if($_POST["students_id"] != '')  
  21.       {  
  22.            $query = "DELETE FROM students WHERE id='".$_POST["students_id"]."'";
  23.            $message = 'Data Updated';  
  24.       }  
  25.  
  26.       if(mysqli_query($connect, $query))  
  27.       {  
  28.            $output .= '<label class="text-success">' . $message . '</label>';  
  29.            $select_query = "SELECT * FROM students ORDER BY id DESC";  
  30.            $result = mysqli_query($connect, $select_query);  
  31.            $output .= '  
  32.                <table class="table">  
  33.                     <tr>  
  34.                          <th width="70%">Name</th>
  35.                     </tr>  
  36.           ';  
  37.            while($row = mysqli_fetch_array($result))  
  38.            {  
  39.                 $output .= '  
  40.                     <tr>  
  41.                          <td>' . $row["name"] . '</td>
  42.                     </tr>  
  43.                ';  
  44.            }  
  45.            $output .= '</table>';  
  46.       }  
  47.       echo $output;  
  48.  }  
  49.  ?>

Código HTML:
Ver original
  1. <!-- botón del index  -->
  2. <td><input type="button" name="del" value="del" id="<?php echo $row["id"]; ?>" class="btn btn-danger btn-xs delete_data" /></td>

Código Javascript:
Ver original
  1. <!-- Añadi el Javascript del view_data renombrando a uno nuevo -->
  2.  <script>
  3.  $(document).on('click', '.delete_data', function(){  //  .view_data
  4.            var students_id = $(this).attr("id");  
  5.            if(students_id != '')  
  6.            {  
  7.                 $.ajax({  
  8.                      url:"delete.php",  // select.php
  9.                      method:"POST",  
  10.                      data:{students_id:students_id},  
  11.                      success:function(data){  
  12.                           $('#students_detail').html(data);
  13.                           $('#insert').val("Eliminar");  
  14.                           $('#dataModal').modal('show');  
  15.                      }  
  16.                 });  
  17.            }            
  18.      });  
  19.  </script>

Código PHP:
Ver original
  1. <?php
  2. // Ahora es un nuevo "delete.php"
  3. // Era "select.php"
  4.  if(isset($_POST["students_id"]))  
  5.  {  
  6.       $output = '';  
  7.       $connect = mysqli_connect("localhost", "root", "", "myDB");  
  8.       $query = "SELECT * FROM students WHERE id = '".$_POST["students_id"]."'";  
  9.       $result = mysqli_query($connect, $query);  
  10.       $output .= '  
  11.      <div class="table-responsive">  
  12.           <table class="table table-bordered">';  
  13.       while($row = mysqli_fetch_array($result))  
  14.       {  
  15.            $output .= '  
  16.                <tr>  
  17.                     <td width="30%"><label>Name</label></td>  
  18.                     <td width="70%">'.$row["name"].'</td>
  19.                     <!-- botón para eliminar  -->
  20.                     <input type="submit" name="insert" id="insert" value="Eliminar" class="btn btn-danger btn-xs delete_data" />
  21.                </tr>  
  22.           ';  
  23.       }  
  24.       $output .= '  
  25.           </table>  
  26.      </div>  
  27.      ';  
  28.       echo $output;  
  29.  }  
  30.  ?>