Can I update a php cycle with a function?

0

This is the code I want to update, it's from an sql query:

 $cadbusca="select  * FROM  tb_ventas_dt WHERE id_venta='".$_SESSION['VENTA_ID_FOLIO']."'";
 $rs = @mysql_query($cadbusca);
 $rows = @mysql_num_rows($rs);

 <table id="tableconcep" border="1" class="table">
     <th>#</th>
     <th>Concepto</th>
     <th>Costo</th>             
     <?while ($registro=@mysql_fetch_array($rs) ){
         echo "<tr>";
         echo "<td class=\"center\"><button type=\"button\" class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\".bd-example-modal-sm\">".$registro['cantidad']."</button> </td>";
         echo "<td class=\"center\">".$registro['producto_ds']." </td>";
         echo "<td class=\"center\"> ".$registro['total']." </td>";
         echo "</tr>";                  
     }?>
 </table>

And when I click on an element I want to update the records

    
asked by GERMAN SOSA 29.05.2018 в 17:11
source

1 answer

0

Using ajax you could update the row you want by sending the id when clicking, for example:

PHP

$cadbusca="select  * FROM  tb_ventas_dt WHERE id_venta='".$_SESSION['VENTA_ID_FOLIO']."'";
$rs = @mysql_query($cadbusca);
$rows = @mysql_num_rows($rs);

 <table id="tableconcep" border="1" class="table">
                        <th>#</th>
                        <th>Concepto</th>
                        <th>Costo</th>

<?while ($registro=@mysql_fetch_array($rs) ) {                                                                                                  
        echo "<tr>";

echo "<td class=\"center\"><button type=\"button\" class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\".bd-example-modal-sm\">".$registro['cantidad']."</button> </td>";
echo "<td class=\"center\">".$registro['producto_ds']." </td>";
echo "<td class=\"center\"> ".$registro['total']." </td>";

// agrego un botón para luego utilizarlo con javascript
echo "<td><button type=\"button\" onclick=\"actualizar_ventas(" . $registro['id_venta'] . ")\">Actualizar</button></td>";

echo "</tr>";                                                                                               
}
?>
</table>

Then in javascript with jQuery

function actualizar_ventas(id_venta) {
  $.ajax({
    // método, puede ser POST, GET, etc
    method: 'POST',
    // url del archivo php donde va a hacer el UPDATE
    url: 'actualizar.php',
    // variables que se le va a pasar como $_POST
    data: { id: id_venta }
  });
}

update.php

<?php
// tomo el valor del id
$id_venta = $_POST['id'];

// ejecuto el update de la fila
$update="UPDATE tb_ventas_dt SET columna = 'actualizo cualquier columna' WHERE id_venta=" . $id_venta;
// puse 'columna' para dar un ejemplo pero ahí deberías poner lo que necesites actualizar

@mysql_query($update);
?>
    
answered by 29.05.2018 в 17:27