jeudi 13 août 2015

Dynamic fields only updating first row


I have a dynamic form (with dynamically adding rows).
This form has 2 normal input fields which send data to table 'orcamen'
and 4 dynamic fields which are stored in table 'tbl_orderdetail'.
When I try to update the dynamically added fields, the table stores only the first row.

This is my edit page that fetches correctly the values from both tables:

<?php 

mysql_connect("localhost","root");
mysql_select_db("alpha");



$id = $_GET["id"];
settype($id, "integer");

    $resultado=mysql_query("SELECT * FROM orcamen WHERE id = $id");

    $orcrow=mysql_fetch_object($resultado);

//This is the table that gets the dynamic rows          
$query = mysql_query( "SELECT * FROM tbl_orderdetail WHERE order_id=$id");

mysql_close();
?>

And here is the html code of the dynamic rows:

<form   method="post" action="salva_orc.php">   
<input type="hidden" name="id" id="id" value="<?php echo $id;?>" />

            <thead>
                <th>No</th>
                <th>Qtde</th>
                <th>Descrição</th>
                <th>Unitário</th>
                <th>Desc.(%)</th>
                <th>Valor</th>
                <th><input type="button" value="+" id="add" class="btn btn-primary"></th>
            </thead>


            <tbody id="orderdetail" class="detail">
            <?php
                while ($result = mysql_fetch_object($query)){
            ?>
                <tr>
                    <td width="2%" class="no">1</td>
                    <td width="10%"><input type="text" class="form-control quantity" name="quantity[$result->id]" value="<?php echo $result->quantity ?>"></td>
                    <td width="60%"><input type="text" class="form-control productname" name="product_name[$result->id]" value="<?php echo $result->product_name ?>"></td>
                    <td width="8%"><input type="text" class="form-control price" name="price[$result->id]" value="<?php echo $result->price ?>"></td>
                    <td width="4%"><input type="text" class="form-control discount" name="discount[$result->id]" value="<?php echo $result->discount ?>"></td>
                    <td width="10%"><input type="text" class="form-control amount" name="amount[$result->id]" value="<?php echo $result->amount   ?>"></td>
                    <td width="6%"><a href="#"  class="remove">Excluir</td>
                </tr>
            <?php } ?>  
</tbody>

            <tfoot>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th style="text-align:center;" >Total  R$</th>
                <th style="text-align:center;" class="total">0</th>
            </tfoot>

        </table>
        <input type="submit" class="btn btn-primary" name="update" id="update" value="Salvar">
        </form> 

<script type="text/javascript">
    $(function(){
        $('#add').click(function(){
        addnewrow();
        });
        $('body').delegate('.remove','click',function(){
            $(this).parent().parent().remove();
        });
        $('.detail').delegate('.quantity,.price,.discount','keyup',function(){
            var tr = $(this).parent().parent();
            var qty = tr.find('.quantity').val();
            var price = tr.find('.price').val();
            var dis = tr.find('.discount').val();
            var amt = (qty * price) - (qty * price * dis)/100;
            tr.find('.amount').val(amt);
            total();
        });
    });

    function total()
    {
    var t = 0;
    $('.amount').each(function(i,e)
    {
        var amt = $(this).val()-0;
        t += amt;
    });
    $('.total').html(t);
    }

    function addnewrow()
    {
        var n = ($('.detail tr').length-0)+1;
        var tr = '<tr>'+
                    '<td class="no">' + n + '</td>'+
                    '<td><input type="text" class="form-control quantity" name="quantity[]"></td>'+
                    '<td><input type="text" class="form-control productname" name="product_name[]"></td>'+
                    '<td><input type="text" class="form-control price" name="price[]"></td>'+
                    '<td><input type="text" class="form-control discount" name="discount[]"></td>'+
                    '<td><input type="text" class="form-control amount" name="amount[]"></td>'+
                    '<td><a href="#" class="remove">Excluir</td>'+
                '</tr>';
        $('.detail').append(tr);        
    }
</script>

And now the update file that is called from the upper form.

<?php 
@ini_set('display_errors', '1');
error_reporting(E_ALL);

mysql_connect("localhost", "root", "");
mysql_select_db("alpha");


$razao     = $_POST["razao"];
$local      = $_POST["local"];
$condicao     = $_POST["condicao"];
$estado         = $_POST["estado"];
$material       = $_POST["material"];
$obs        = $_POST["obs"];
$id         = $_POST["id"];
mysql_query ("UPDATE orcamen SET razao='$razao' , local='$local' , condicao='$condicao' , estado='$estado' , material='$material' , obs='$obs' WHERE id=$id");


foreach ($_POST['quantity'] as $ord_det_id => $quantity) {
$product_name = $_POST['product_name'][$ord_det_id];
$price = $_POST['price'][$ord_det_id];
$discount = $_POST['discount'][$ord_det_id];
$amount = $_POST['amount'][$ord_det_id];

mysql_query ("UPDATE tbl_orderdetail SET product_name='$product_name',  quantity='$quantity', price='$price', discount='$discount', amount='$amount'    WHERE order_id = $id");
}


mysql_close();
header("Location: consulta_orc.php");

?>

I hope someone can help me solve this.

The new html code:

<?php
    while ($result = mysql_fetch_object($query)){
?>
<tr>
    <td width="2%" class="no">1</td>
    <td width="10%"><input type="text" class="form- control quantity" name="quantity[<?php echo $result->id ?>]" value="<?php echo $result->quantity ?>"></td>
    <td width="60%"><input type="text" class="form-control productname" name="product_name[<?php echo $result->id ?>]" value="<?php echo $result->product_name ?>"></td>
    <td width="8%"><input type="text" class="form-control price" name="price[<?php echo $result->id ?>]" value="<?php echo $result->price ?>"></td>
    <td width="4%"><input type="text" class="form-control discount" name="discount[<?php echo $result->id ?>]" value="<?php echo $result->discount ?>"></td>
    <td width="10%"><input type="text" class="form-control amount" name="amount[<?php echo $result->id ?>]" value="<?php echo $result->amount ?>"></td>
    <td width="6%"><a href="#" class="remove">Excluir</td>
</tr>
<?php } ?>  

And the php code with var_dump:

<?php 
@ini_set('display_errors', '1');
error_reporting(E_ALL);

mysql_connect("localhost", "root", "");
mysql_select_db("alpha");


$razao     = $_POST["razao"];
$local      = $_POST["local"];
$condicao     = $_POST["condicao"];
$estado         = $_POST["estado"];
$material       = $_POST["material"];
$obs        = $_POST["obs"];
$id         = $_POST["id"];
mysql_query ("UPDATE orcamen SET razao='$razao' , local='$local' , condicao='$condicao' , estado='$estado' , material='$material' , obs='$obs' WHERE id=$id");


foreach ($_POST['quantity'] as $ord_det_id => $quantity) {
$order_id = $_POST['order_id'][$ord_det_id];    
$product_name = $_POST['product_name'][$ord_det_id];
$price = $_POST['price'][$ord_det_id];
$discount = $_POST['discount'][$ord_det_id];
$amount = $_POST['amount'][$ord_det_id];



mysql_query ("UPDATE tbl_orderdetail SET product_name='$product_name', quantity='$quantity', price='$price', discount='$discount', amount='$amount' WHERE order_id = $id");



}



header("Location: consulta_orc.php");
?>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire