使用错误ID

时间:2015-08-17 01:00:10

标签: php mysql


我正在尝试使用动态行更新表单,其中,在更新之后,order_id必须保持不变。
我的代码中发生的事情是,每次更新时,每个添加或更改的行都会获得后续的order_id 例如,第1行有order_id 20和第2行,而不是获得order_id 20,获得21 我希望有一个人可以帮助我。我一直都没有解决方案 我上传了[图片] [1],它解释了所发生的一切。   [1]:http://imgur.com/VHECzC9
我有一种强烈的感觉,'id'有问题。请参阅下面的表格结构:

表orcamen(无动态字段值):

`id` int(11) NOT NULL AUTO_INCREMENT,
`razao` varchar(255) DEFAULT NULL,
`local` varchar(255) DEFAULT NULL,
`condicao` varchar(255) DEFAULT NULL,
`estado` enum('Aberto','Aprovado','Cancelado') DEFAULT NULL,
`material` varchar(255) DEFAULT NULL,
`obs` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)

表tbl_orderdetail(获取动态行):

`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) DEFAULT NULL,
`product_name` varchar(255) DEFAULT NULL,
`quantity` varchar(255) DEFAULT NULL,
`price` varchar(255) DEFAULT NULL,
`discount` int(11) DEFAULT NULL,
`amount` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)

这是我的编辑页面,可正确获取两个表中的值:

<?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();
?>

这是动态行的html代码:

<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[<?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 } ?>  

动态添加行的脚本:

<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>

现在是从上层表单调用的更新文件。

<?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");

}

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

0 个答案:

没有答案