How to insert several products, in different registers in the same table?

-2

Good afternoon, dear friends, I need you to please help me if possible with this problem that I can not find a solution for, if it is not an inconvenience, and thanks in advance for your help, advice, or guidance.

The issue is that I am making a Point of Sale (POS), and I am developing the module of purchases of the system, this module is raised in ajax because the process is as follows:

  • When clicking on the purchase button, all the records in the HTML table must be inserted into the database by ajax and this is where my question comes from.
  • How to do so that when making the purchase, the products that I bought are registered in the database with the same transaction number but in different rows? Ex:

    For 3 products of the HTML table, loaded with the INFO of the form.

    ID | NROTRANS | PRODUCTO
    1        1         1
    2        1         2
    3        1         3
    

    SIMPLE SOLUTION

    Well I finally found the solution, what I did was to change the logic of product loading a bit, before you had to load all the products to the table through jquery, and at the end of the purchase is to insert the multiple rows to the database.

    What I did was that now every time I fill out the form, I submit, I rescue by getting the $ _SESSION from the supplier I'm buying from and with header location redirected with that parameter to the same page, and I evaluate if the parameter is present in the URL, if so, I charge all the products associated with that supplier from the table acsi_posprecompra, and ready them on a table.

    At the end of the purchase, the records associated with that provider are searched in the table acsi_posprecompra, the records are traversed by a while loop and inserted sequentially in the acsi_postransacciones table, after having finished the process, a DELETE is made to all records associated with that provider in the acsi_posprecompra table.

    Sorry for not placing all the code, you will understand that it is extensive ...

    Thanks to Hammerfall for responding, I tried it as you proposed but in the end it was not what they wanted ... But it was still a valid answer, work with arrays.

        
    asked by Alejandro Monzón 05.03.2018 в 19:37
    source

    1 answer

    0

    It's not accurate, but you can get an idea

    Given the next row

    var fila = '<tr id="row' + i + '"> <td>' + nit + '</td>'+
    '<td>' + codProd + '</td> <td>' + descProd + '</td>'+
    '<td>' + qty + '</td> <td>' + price + '</td>' + 
    '<td><button type="button" name="remove" id="' + i + '" class="btn btn-danger btn_remove"><i class="fa fa-trash-alt"></i> Eliminar</button></td></tr>'; //esto seria lo que contendria la fila
    

    you can leave it in the following way

    var fila = '<tr id="row' + i + '">'+
    '<td>' + nit + ' </td>'+
    '<td><input type="hidden" name="productosId[]" value="'+codProd+'" />' + codProd + '</td>'+
    '<td><input type="hidden" name="descripcion[]" value="'+descProd+'" />' + descProd + '</td>'+
    '<td><input type="hidden" name="cantidades[]" value="'+qty+'" />' + qty + '</td>'+
    '<td><input type="hidden" name="precios[]" value="'+price+'" />' + price + '</td>' + 
    '<td><button type="button" name="remove" id="' + i + '" class="btn btn-danger btn_remove"><i class="fa fa-trash-alt"></i> Eliminar</button></td></tr>'; //esto seria lo que contendria la fila
    

    With this we add more fields to the form, in the form of an array, this means that we can read from $_POST the product arrayId

    For practical purposes, the transaction table should not carry the PRODUCT, CANTIDE or PRICE fields. I do not know why you have it that way, but given the insertion of the transaction, you can insert the products in the following way

    // aqui se insertaria la transacción
    
    $sql = "INSERT INTO transacciones VALUES(.......)";
    $result = mysqli_query($link,$sql);
    
    // obtenemos el id de la transacción insertada, 
    $idTransaccion = mysqli_insert_id($link);
    $productoId = 0;
    $descripcion = 0; // es el nombre del producto
    $cantidad = 0;
    $precio = 0;
    //Insertariamos los movimientos de la transacción obteniendo los datos de los productos uno por uno
    // suponiendo que tu tabla de movimientos tenga los campos id(auto_increment), idtransaccion, productoid, cantidad, descripcion y precio
    // y todos los campos son enteros o flotantes
    // http://php.net/manual/es/mysqli-stmt.prepare.php
    // http://php.net/manual/es/mysqli-stmt.bind-param.php
    $stmt = mysqli_prepare($link, "INSERT INTO movimientos VALUES(?,?,?,?,?)");
    $stmt->bind_param('iiidd', $idTransaccion, $productoId, $cantidad, descripcion, $precio); // 
    foreach($_POST['productosId'] as $key => productoId) {
        $descripcion = $_POST['descripcion'][$key];
        $cantidad = $_POST['cantidades'][$key];
        $precio = $_POST['precios'][$key];
    
        $stmt->execute();
    }
    $stmt->close();
    
        
    answered by 05.03.2018 / 21:59
    source