如何将多个数据插入表中

时间:2015-08-19 03:51:32

标签: php jquery mysql arrays

我有一个表,每个行项目都有一个addtocart按钮,当我点击addtocart按钮,一个模态显示它有我想要订购的数量的确定按钮,当我点击确定按钮时,另一个模态将显示它有像第一个表一样的表格形式,它显示了我订购的项目,当我点击确定按钮时,我希望将这些行插入数据库。

$( $table ).delegate(".Addtocart", "click", function() {
                var itemNo = $(this).closest('tr').find('td.itemNo').html();
                var selected_item = $(this).closest('tr').find('td.itemdescr').html();
                var remaining_qty = $(this).closest('tr').find('td.currqty').html();
                var unitPrice = $(this).closest('tr').find('td.unit_price').html();
                var amount = $(this).closest('tr').find('td.Amount').html();
                $("#action").val('add');
                $('#itemNo').val(itemNo);
                $('#selected_item').val($.trim(selected_item));
                $('#remaining_qty').val(remaining_qty);
                $('#unitPrice').val(unitPrice);
                $('#amount').val(amount);
                $('#myModal').modal('show');
            }); 

继承第二个模态的第二个确定按钮

$('#OKBtn2').click(function(){
            $('#myModal2').modal('hide');
            var itemid = $('#main-form2 .active').attr('id'),
                qty = $('#main-form2 #'+itemid+' td:eq(2)').text(),
                amount = $('#main-form2 #'+itemid+' td:eq(4)').text();
                bootbox.confirm("Are you sure?","No","Yes",function(r){
                    if(r) {
                        var itemno = $('#itemNo').val();
//                        var currqty = $('#orderqty').val();
                        var unit_price = $('#unitPrice').val();
//                        var Amount = $('#amount').val();
                        $.ajax({  
                            url : url,
                            type : "POST",
                            async : false,
                            data : {
                                    Modify: 1,
                                    Delete:1,
                                    Add: 1,
                                    itemNo: itemno,
                                    orderqty: qty,
                                    unit_price: unit_price,
                                    amount: amount,
                                    todo:"Add"
                                       },
                        success:function(result){
                        bootbox.alert('Ordered',function(){
                        });
                        updateTable();
                        }
                    });

                    } else {

                    }
                });
            });

如何将其转换为数组?数据插入到表中,但它只插入选中或突出显示的行项,但不插入其他行项。该行具有类,当您单击它时突出显示该行。

case "Add":
        if(isset($_POST['Add'])){
                    $Addquery = "INSERT INTO tb_empgrocery (empgrocID, coopmemID , date_ordered, item_no, qty_ordered, unit_price, amount, date_delivered, qty_delivered, order_status, released_by) VALUES ('".$_POST['empgrocID']."', '".$login['is_coopmemID_kiosk']."', (NOW()), '".$_POST['itemNo']."', '".$_POST['orderqty']."', '".$_POST['unit_price']."', '".$_POST['amount']."', '".$_POST['date_delivered']."', '".$_POST['qty_delivered']."','".$_POST['order_status']."','".$_POST['released_by']."')";
                    mysql_query($Addquery, $con);
                    }
        break;

这是正确的吗?

case "Add":
            $_POST = array_map('addlashes', $_POST);
            $values = array();
                foreach ($_POST as $key => $value) {
                    $values[] = "('{$_POST['empgrocID']}', '{$login['is_coopmemID_kiosk']}', '{$_POST['(NOW())']}', '{$_POST['itemNo']}', '{$_POST['orderqty']}', '{$_POST['unit_price']}', '{$_POST['amount']}', '{$_POST['date_delivered']}', '{$_POST['qty_delivered']}', '{$_POST['order_status']}', '{$_POST['released_by']}')";
                }
                if(sizeof($values)) {
                    $query = "INSERT INTO tb_empgrocery (empgrocID, coopmemID , date_ordered, item_no, qty_ordered, unit_price, amount, date_delivered, qty_delivered, order_status, released_by) VALUES ".implode(',', $values);
                    $result = mysql_query($query, $con);
                }
        break;

提前致谢

1 个答案:

答案 0 :(得分:0)

$ _ POST本身就是一个数组,只要发布数据键与表列匹配,就可以在表中插入数据。