|
 |
|
รบกวนสอบถามค่ะ จากเดิมสามารถรวมค่า ดึงค่า ใน textbox แบบอัตโนมัติได้ แต่พอเพิ่มจำนวนแล้วทำไม่ได้ค่ะ |
|
 |
|
|
 |
 |
|
รบกวนสอบถามค่ะ ไม่ทราบว่าจะต้องแก้ตรงไหน
จากเดิม

ตารางข้อมูล
member

product

bill

billdetails

เมื่อเลือกสินค้า ราคาสินค้าจะถูกดึงในช่องโดยอัตโนมัติ และเมื่อใส่จำนวน ช่องรวมเงินก็จะโชว์โดยอัตโนมัติ และสามารถเพิ่มข้อมูลลงฐานข้อมูลได้ แต่พอต้องการให้มีรายการสินค้าหลายๆแถว กลับไม่เป็น auto ค่ะ
และจากเดิมไม่มีตาราง billdetails (ข้อมูลในตาราง billdetails ของเดิมอยู่ใน bill) พอต้องการให้รับสินค้าได้หลายรายการ จึงเพิ่ม ตาราง billdetails ขึ้นมา แต่ก็ไม่ทราบว่า จะ insert ข้อมูลอย่างไร
ของใหม่ที่ต้องการ

Codeหน้า bill.php
Code (PHP)
<?php
include 'dbconn.php';
mysql_query("SET NAMES utf8");
$sql = "select * from member ORDER BY idM ASC";
$dbquery = mysql_query($sql);
while ($rw = mysql_fetch_array($dbquery)) {
$opt_idM.="<option value='$rw[idM]' >$rw[add1] $rw[nameM]</option>\n";
}
$sql = "select * from product ORDER BY idP ASC";
$dbquery = mysql_query($sql);
$opt_idP = '';
$price = 0;
$idp_list = array(); // ตัวแปรเก็ราคา คิวรี่่ชื่อสินค้า รหัสสินค้าแล้ว ก็เอาราคามาใช้ด้วยเลย
while ($rw = mysql_fetch_array($dbquery)) {
$idp_list[$rw[idP]] = $rw; // set price list
if ($_GET["item"] == $rw["idP"]) {
$sel = "selected";
$price = $rw['price'];
} else {
$sel = "";
}
$opt_idP.="<option value='$rw[idP]' $sel >$rw[idP] $rw[nameP]</option>\n";
}
?>
<html>
<script language="JavaScript">
var prize_list = <?= json_encode($idp_list) ?>;
// สร้างตัวแปรเก็บราคา เมื่อม้ันคิวรี่เพื่อ ทำ dropdown list แล้วก็ใช้ประโยน์ จากตรงนี้เลย
function chk() {
var price = parseInt(document.frm.uPrice.value);
var amt = parseInt(document.frm.amount.value);
document.frm.total.value = price * amt; //---- เปลี่ยนเอาจะ + - * /
}
function set_price(id) {
document.frm.uPrice.value = prize_list[document.frm.idP.value].price;
}
</script>
<body>
<?php include 'header.php'; ?>
<div>
<div>
<ul class="nav nav-pills">
<li><a href="member.php">สมาชิก</a></li>
<li><a href="product.php">สินค้า</a></li>
<li class="active"><a href="bill.php">รับซื้อสินค้า</a></li>
<li><a href="index.php">ออกจากระบบ</a></li>
</ul>
<hr></div>
<center>
<center><font color="red"><b>เพิ่มข้อมูลบิล</b></font></center>
<form action="billAdd.php" method="post" name="frm">
<table>
<tr>
<td>สมาชิก :</td>
<td>
<select class="form-control" name="idM" id="idM" style="width:400px">
<option value=""><-- Please Select Item --></option>
<?= $opt_idM ?>
</select>
</td></tr>
</table><br>
<table>
<tr>
<td>สินค้า :</td>
<td>
<select class="form-control" name="idP" id="idP" style="width:250px" OnChange="set_price()">
<option value=""><-- Please Select Item --></option>
<?= $opt_idP ?>
</select>
</td>
<td> ราคาต่อหน่วย :</td>
<td><input class="form-control" name="uPrice" type="text" value="<?= $price ?>" readonly style="width:70px"></td>
<td> จำนวน :</td>
<td><input class="form-control" type="text" name="amount" style="width:100px" onChange="chk()"></td>
<td> รวมเงิน :</td>
<td><input class="form-control" type="text" name="total" style="width:100px" ></td>
</tr>
<tr>
<td>สินค้า :</td>
<td>
<select class="form-control" name="idP" id="idP" style="width:250px" OnChange="set_price()">
<option value=""><-- Please Select Item --></option>
<?= $opt_idP ?>
</select>
</td>
<td> ราคาต่อหน่วย :</td>
<td><input class="form-control" name="uPrice" type="text" value="<?= $price ?>" readonly style="width:70px"></td>
<td> จำนวน :</td>
<td><input class="form-control" type="text" name="amount" style="width:100px" onChange="chk()"></td>
<td> รวมเงิน :</td>
<td><input class="form-control" type="text" name="total" style="width:100px" ></td>
</tr>
<tr>
<td>สินค้า :</td>
<td>
<select class="form-control" name="idP" id="idP" style="width:250px" OnChange="set_price()">
<option value=""><-- Please Select Item --></option>
<?= $opt_idP ?>
</select>
</td>
<td> ราคาต่อหน่วย :</td>
<td><input class="form-control" name="uPrice" type="text" value="<?= $price ?>" readonly style="width:70px"></td>
<td> จำนวน :</td>
<td><input class="form-control" type="text" name="amount" style="width:100px" onChange="chk()"></td>
<td> รวมเงิน :</td>
<td><input class="form-control" type="text" name="total" style="width:100px" ></td>
</tr>
<tr>
<td>สินค้า :</td>
<td>
<select class="form-control" name="idP" id="idP" style="width:250px" OnChange="set_price()">
<option value=""><-- Please Select Item --></option>
<?= $opt_idP ?>
</select>
</td>
<td> ราคาต่อหน่วย :</td>
<td><input class="form-control" name="uPrice" type="text" value="<?= $price ?>" readonly style="width:70px"></td>
<td> จำนวน :</td>
<td><input class="form-control" type="text" name="amount" style="width:100px" onChange="chk()"></td>
<td> รวมเงิน :</td>
<td><input class="form-control" type="text" name="total" style="width:100px" ></td>
</tr>
<tr>
<td>สินค้า :</td>
<td>
<select class="form-control" name="idP" id="idP" style="width:250px" OnChange="set_price()">
<option value=""><-- Please Select Item --></option>
<?= $opt_idP ?>
</select>
</td>
<td> ราคาต่อหน่วย :</td>
<td><input class="form-control" name="uPrice" type="text" value="<?= $price ?>" readonly style="width:70px"></td>
<td> จำนวน :</td>
<td><input class="form-control" type="text" name="amount" style="width:100px" onChange="chk()"></td>
<td> รวมเงิน :</td>
<td><input class="form-control" type="text" name="total" style="width:100px" ></td>
</tr>
</table>
<input type="submit" value="เพิ่ม">
</form><br></center>
<!--แถวที่ 2--><!--เนื้อหา-->
</div><!--End แถวที่ 2-->
โค๊ด insert
Code (PHP)
<?php
include 'dbconn.php';
$idB = $_POST['idB'];
$idM= $_POST['idM'];
$idP= $_POST['idP'];
$amount= $_POST['amount'];
$price= $_POST['price'];
$total = $_POST['total'];
$insert = "INSERT INTO bill (date, idM, ) VALUES (NOW(), '$idM');";
mysql_query("SET NAMES utf8");
$result = mysql_query($insert);
$insert1 = "INSERT INTO billdetails (idB, idP, amount, price, total) VALUES ('$idB', '$idP', '$amount', '$price', '$total');";
mysql_query("SET NAMES utf8");
$result = mysql_query($insert1);
mysql_close($conn);
echo "<script type='text/javascript'>window.location.href = \"bill.php\";</script>";
?>
Tag : PHP

|
ประวัติการแก้ไข 2014-11-27 18:25:45
|
 |
 |
 |
 |
Date :
2014-11-27 18:20:34 |
By :
ro_1236 |
View :
942 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้แบบนี้ดึงค่าไม่ได้น่ะครับ เพราะถ้าช่องใดช่องหนึ่งว่าง Index จะเปลี่ยนทันทีครับ แนะนำให้ทำหนดชื่อ name ที่ id ของแต่ล่ะ rows ระบุครับ

PHP MySQL Add/Insert Multiple Record
|
 |
 |
 |
 |
Date :
2014-11-28 11:27:42 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|