Mọi người ơi, em đang làm form hóa đơn. Em insert dữ liệu vào 2 bảng là hóa đơn và chi tiết hóa đơn. Em đã insert vào bảng hóa đơn (mã hoa don, ngày lập, nhan vien) thì được rồi. nhưng câu insert thứ 2 là insert vào bảng chi tiết hoa đơn thì lại không được mong mọi người giúp đỡ.
(Mình ngĩ là do mình không bắt được id bên mã món bên bảng chi tiết hóa đơn á. nên nó không thể hiểu vào thêm vào được)
file index.php form hóa đơn
<form name="luuhd" id="luuhd" action="modules/demo-table/xuly.php" method="post" enctype="multipart/form-data">
<h2 style="text-align:center;">Danh sách món trong hóa đơn</h2>
<h5 style="margin-top:10px;margin-bottom:10px;">
Mã HĐ: <input name="mahd" type="text" readonly="readonly" id="mahd" size="3" value="<?php
$r = mysql_query("show table status like 'hoadon' ");
$row = mysql_fetch_array($r);
echo $row['Auto_increment'];
?>"/>
Ngày lập: <?php $ngaylap = +7;
echo '<input type="text" name="ngaylap" size = "20" readonly="readonly" id="ngaylap" value="'.gmdate("HA:i:s | d-m-Y", time() + 3600*($ngaylap + date("I"))).'">';
?>
Nhân viên: <input name="nhanvien" type="text" id="nhanvien" readonly="readonly" value="<?php echo $_SESSION['currTen']?>" size="17"/>
</h5>
<!-- Bảng chi tiết trong hóa đơn -->
<table class="table table-bordered table-hover" style="margin-left:auto;margin-right:auto;">
<thead>
<tr>
<th>STT</th>
<th>Mã món</th>
<th>Tên món</th>
<th>Đơn giá</th>
<th>Số lượng</th>
<th>Thành tiền</th>
<th>Xóa</th>
<th>Lưu</th>
</tr>
</thead>
<tbody id="bill-table" class="chitiet">
<tr>
<td colspan="8" class="empty">Chưa chọn món</td>
</tr>
</tbody>
<tfoot>
<th></th>
<th></th>
<th></th>
<th></th>
<th>Tổng tiền:</th>
<th style="text-align:center"; class="total" ></th>
<th></th>
<th></th>
</tfoot>
</table>
</form>
<!--
<h4 style="text-align:center;margin-top:10px;margin-bottom:10px;">Tổng tiền:<input name="tongtien" type="text" id="tongtien" class ="total" value="" size="20" />-->
<input type="submit" name="luuhd" id="luuhd" value=" Lưu " />
<input type="submit" name="inhd" value=" In Hóa Đơn " />
</h4>
</form>
</div>
<script type="text/javascript">
//hàm của nút delete
$(function(){
$('.right').delegate('.delete','click',function(){
$(this).parent().parent().remove();
});
});
//ham luu csdl
/*$(function(){
$('.chitiet').delegate('.savect','click',function(){
//luu o day
});
})
*/
//ham tinh tong tien
function total(){
var t= 0;
$('.bill-total').each(function(i, e) {
var amt = $(this).html()-0;
t += amt;
});
$('.total').html(t);
}
//ham tinh tong
(function() {
var index = 1;
$('.btn-choose').on('click', choose);
$('#bill-table').on('change keyup paste', '.bill-quantity', changeValue);
function choose() {
var self = this,
bill = $('#bill-table'),
data = getDataMenu($(self).parent().parent()),
html = createRow(data);
if (bill.find('.empty').length) {
bill.empty();
}
bill.append(html);
}
function changeValue() {
var self = this,
row = $(self).parent().parent(),
data = getDataBill(row);
row.find('.bill-total').html(calculate(data.price, data.quantity));
}
function getDataMenu(row) {
var id = $(row.find('.col-id')).html(),
name = $(row.find('.col-name')).html(),
price = $(row.find('.col-price')).html();
var data = {
id: id,
name: name,
price: price
}
return data;
}
function getDataBill(row) {
var price = $(row.find('.bill-price')).html(),
quantity = $(row.find('.bill-quantity')).val();
var data = {
price: price,
quantity: quantity
}
return data;
}
function createRow(data) {
var html = '<tr>';
html += '<td>' + index + '</td>';
html += '<td><input type="text" id="col-id" name="col-id" size="3" readonly="readonly" value="' + data.id + '" /></td>';
html += '<td id="col-name">' + data.name + '</td>';
html += '<td class="bill-price" id="bill-price">' + data.price + '</td>';
html += '<td><input type="text" value="1" class="bill-quantity" name="bill-quantity" size = "3" id="bill-quantity" /></td>';
html += '<td class="bill-total" id="bill-total">' + calculate(data.price, 1) + '</td>';
html += '<td><input type="submit" value="Delete" class="delete" name="delete"/></td>';
html += '<td></td>';
html += '</tr>';
return html;
}
function calculate(price, quantity) {
return price * quantity;
}
})();
</script>
</div>
form xử lý đê insert dữ liệu
<?php
include ("../../../Connections/conf.php");
//dữ liệu của bảng hóa đơn
//$mahd= $_GET["mahd"];
$ngaylap = $_GET["ngaylap"];
$nhanvien = $_GET["nhanvien"];
if(isset($_POST["luuhd"])){
mysql_query( "insert into hoadon (ngaylap,manv)
value('$ngaylap','$nhanvien')");
$id = mysql_insert_id();
mysql_query("INSERT INTO chitiethd
SET mahd = '{$id}',
mamon = '{$_POST['col-id'][$i]}',
soluong = '{$_POST['bill-quantity'][$i]}'
");
header("location: ../../../manager/index.php?quanly=hoadon&ac=themhd");
}
?>