ขอคำแนะนำเรื่องการบันทึกข้อมูลจากหน้ารายละเอียดสินค้า ไปหน้ารายการสินค้าและฐานข้อมูลสินค้าครับ
คือ จะให้ข้อมูลจำนวนสินค้าที่ซื้อเข้ามาที่เก็บไว้ใน รายละเอียดการซื้อเข้า (purchase_detail)
โดยเอาข้อมูลจำนวนสินค้าทั้งหมดไป Insert ไว้ในตาราง product ฟิล product_qty
ของแต่ละ ID สินค้าทั้งในหน้าเว็บและในฐานข้อมูลครับ
อยากทราบต้องใช้วิธีแบบไหนอย่างไรครับ
ฐานข้อมูล purchase_detail
code purchase_detail.php
Code (PHP)
<?php require_once('Connections/MyConnect.php'); ?>
<?php require_once('Connections/MyConnect.php');
mysql_select_db($database_MyConnect, $MyConnect);
$pd=intval($_REQUEST['product_id']);
$SQL="select
pur_detail_id id,
product_name name,
DATE_FORMAT(pur_detail_date, '%d-%m-%Y') pur_date,
pur_detail_principal pur_princ,
pur_detail_productqty product_qty
from
purchase_detail INNER JOIN product
ON purchase_detail.product_id = product.product_id
WHERE product.product_id=$pd
";
$QUERY= mysql_query($SQL) or die ("Error Query [".$SQL."]".mysql_error());
$sql .= "ORDER BY `product_id` ASC ";
?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && true) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "admin/index.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_MyConnect, $MyConnect);
$query_Recordset1 = "SELECT * FROM purchase_detail";
$Recordset1 = mysql_query($query_Recordset1, $MyConnect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$maxRows_purchaseset1 = 5;
$pageNum_purchaseset1 = 0;
if (isset($_GET['pageNum_purchaseset1'])) {
$pageNum_purchaseset1 = $_GET['pageNum_purchaseset1'];
}
$startRow_purchaseset1 = $pageNum_purchaseset1 * $maxRows_purchaseset1;
$query_purchaseset1 = "SELECT * FROM purchase_detail";
$query_limit_purchaseset1 = sprintf("%s LIMIT %d, %d", $query_purchaseset1, $startRow_purchaseset1, $maxRows_purchaseset1);
$purchaseset1 = mysql_query($query_limit_purchaseset1, $MyConnect) or die(mysql_error());
$row_purchaseset1 = mysql_fetch_assoc($purchaseset1);
if (isset($_GET['totalRows_purchaseset1'])) {
$totalRows_purchaseset1 = $_GET['totalRows_purchaseset1'];
} else {
$all_purchaseset1 = mysql_query($query_purchaseset1);
$totalRows_purchaseset1 = mysql_num_rows($all_purchaseset1);
}
$totalPages_purchaseset1 = ceil($totalRows_purchaseset1/$maxRows_purchaseset1)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>purchase_detail</title>
<script src="js/jquery.js" type="text/javascript"></script>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<style type="text/css">
#MenuBar1 {
font-size: 230%;
font-family: "Courier New", Courier, monospace;
}
body p {
font-size: 18px;
text-align: center;
color: #000;
}
body h2 {
text-align: center;
}
</style>
<body class='container'>
<p><img src="shop.jpg" width="1117" height="149" />
</p>
<h3>ร้านเกสร
</h3>
<p>ระบบจัดการฐานข้อมูลสำหรับร้านค้าปลีก</p>
<div class="btn-group">
<button class="btn"><a href="sales_detail.php">ขายสินค้า</a></button>
</div>
<div class="btn-group">
<button class="btn">ข้อมูลพื้นฐาน</button>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="Product.php">ข้อมูลสินค้า</a></li>
<li><a href="Category.php">ข้อมูลประเภทสินค้า</a></li>
<li><a href="Supplier.php">ข้อมูลผู้จัดจำหน่าย</a></li>
<!-- dropdown menu links -->
</ul>
</div>
<div class="btn-group">
<button class="btn"><a href="Unit.php">หน่วยสินค้า</a></button>
</div>
<div class="btn-group">
<button class="btn"><a href="repayment">จัดการลูกหนี้</a></button>
</div>
<div class="btn-group">
<button class="btn">เพิ่มเติม</button>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="#">ดูสินค้าใกล้หมด</a></li>
<li><a href="#">เช็คสต๊อกสินค้า</a></li>
<!-- dropdown menu links -->
</ul>
</div>
<div class="btn-group">
<button class="btn"><a href="edit-profile.php">แก้ไขข้อมูลส่วนตัว</a></button>
</div>
<div class="btn-group">
<button class="btn"><a href="admin/index.php">ออกจากระบบ</a></button>
</div>
<p> </p>
<p>รายละเอียดการซื้อเข้า</p>
<p>
<a href="addpurchase_detail.php">
<input type="submit" name="btn" id="btn" value="เพิ่ม" />
</a></p>
<table table class="table table-bordered" width="105%" border="1">
<tr class="success">
<td width="196">รหัส</td>
<td width="196">ชื่อสินค้า</td>
<td width="213">วันที่ซื้อเข้า</td>
<td width="238">ราคาต้นทุน</td>
<td width="252">จำนวนสินค้าที่ซื้อเข้า</td>
<td width="55">แก้ไข</td>
<td width="55">ลบ</td>
</tr>
<?php
$Total = 0;
$SumTotal = 0;
while($rs=mysql_fetch_array($QUERY)) {
$Total = $rs['product_qty'];
$SumTotal = $SumTotal + $Total;
?>
<tr>
<td><?php echo $rs['id']; ?></td>
<td><?php echo $rs['name']; ?></td>
<td><?php echo $rs['pur_date']; ?></td>
<td><?php echo $rs['pur_princ']; ?></td>
<td><?php echo $rs['product_qty']; ?></td>
<td><i class="icon-pencil"></i><a href="editpurchase_detail.php?pur_detail_id =<?php echo $rs['id']; ?>">แก้ไข</a></td>
<td><i class="icon-trash"></i><a onclick="return confirm_delete()" href="deletePurchase_detail.php?product_id=<?php echo $rs['product_id']; ?>">ลบ</a></td>
<?php } $rs= mysql_fetch_assoc($purchaseset1); ?>
</table>
<td align="right">จำนวนสินค้าทั้งหมด <?php echo number_format($SumTotal);?> ชิ้น</td>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
function confirm_delete() {
return confirm('ยืนยันการลบข้อมูล !');
}
</script>
<?php
mysql_free_result($purchaseset1);
mysql_free_result($Recordset1);
?>
ฐานข้อมูล Product
code Product.php
Code (PHP)
<?php require_once('Connections/MyConnect.php');
session_start();
mysql_select_db($database_MyConnect, $MyConnect);
$SQL="SELECT * FROM product
LEFT JOIN category ON (product.cate_id=category.cate_id)
LEFT JOIN unit ON (product.unit_id=unit.unit_id)";
$QUERY= mysql_query($SQL) or die ("Error Query [".$SQL."]".mysql_error());
$sql .= "ORDER BY `product_id` ASC ";
?>
<?php
if (!isset($_SESSION)) {
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && true) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "admin/index.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$maxRows_Productset = 5;
$pageNum_Productset = 0;
if (isset($_GET['pageNum_Productset'])) {
$pageNum_Productset = $_GET['pageNum_Productset'];
}
$startRow_Productset = $pageNum_Productset * $maxRows_Productset;
$query_Productset = "SELECT * FROM product";
$query_limit_Productset = sprintf("%s LIMIT %d, %d", $query_Productset, $startRow_Productset, $maxRows_Productset);
$Productset = mysql_query($query_limit_Productset, $MyConnect) or die(mysql_error());
$row_Productset = mysql_fetch_assoc($Productset);
if (isset($_GET['totalRows_Productset'])) {
$totalRows_Productset = $_GET['totalRows_Productset'];
} else {
$all_Productset = mysql_query($query_Productset);
$totalRows_Productset = mysql_num_rows($all_Productset);
}
$totalPages_Productset = ceil($totalRows_Productset/$maxRows_Productset)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Product</title>
<style type="text/css">
#MenuBar1 {
font-size: 230%;
font-family: "Courier New", Courier, monospace;
}
body p {
font-size: 18px;
text-align: center;
color: #000;
}
body h2 {
text-align: center;
}
#form1 #word {
text-align: left;
}
#form1 #btnSearch {
text-align: center;
}
#form1 {
text-align: center;
}
</style>
<script src="js/jquery.js" type="text/javascript"></script>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body class='container'>
<p><img src="shop.jpg" width="1117" height="149" />
</p>
<h3>ร้านเกสร
</h3>
<p>ระบบจัดการฐานข้อมูลสำหรับร้านค้าปลีก</p>
<div class="btn-group">
<button class="btn"><a href="sales_detail.php">ขายสินค้า</a></button>
</div>
<div class="btn-group">
<button class="btn">ข้อมูลพื้นฐาน</button>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="Product.php">ข้อมูลสินค้า</a></li>
<li><a href="Category.php">ข้อมูลประเภทสินค้า</a></li>
<li><a href="Supplier.php">ข้อมูลผู้จัดจำหน่าย</a></li>
<!-- dropdown menu links -->
</ul>
</div>
<div class="btn-group">
<button class="btn"><a href="Unit.php">หน่วยสินค้า</a></button>
</div>
<div class="btn-group">
<button class="btn"><a href="repayment">จัดการลูกหนี้</a></button>
</div>
<div class="btn-group">
<button class="btn">เพิ่มเติม</button>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="#">ดูสินค้าใกล้หมด</a></li>
<li><a href="#">เช็คสต๊อกสินค้า</a></li>
<!-- dropdown menu links -->
</ul>
</div>
<div class="btn-group">
<button class="btn"><a href="edit-profile.php">แก้ไขข้อมูลส่วนตัว</a></button>
</div>
<div class="btn-group">
<button class="btn"><a href="admin/index.php">ออกจากระบบ</a></button>
</div>
<p> </p>
<form id="form1" name="form1" method="post" action="searchproduct.php">
<label for="word">ค้นหา:<i class="icon-zoom-in"></i></label>
<input type="text" name="word" id="word" />
<input type="submit" name="btnSearch" id="btnSearch" value="ค้นหา" />
</form>
<p>
<a href="insertproduct.php">
<input type="submit" name="btn" id="btn" value="เพิ่มสินค้า" /></a></p>
<p>แสดงสินค้า </p>
<table class="table table-bordered" width="500" border="1" align="center">
<tr class="success">
<td width="159">รหัสสินค้า</td>
<td width="181">ชื่อสินค้า</td>
<td width="136">ประเภทสินค้า</td>
<td width="132">หน่วยสินค้า</td>
<td width="195">คงเหลือต่ำสุด</td>
<td width="179">ราคา</td>
<td width="179">จำนวนสินค้า</td>
<td width="27">แก้ไข</td>
<td width="27">ลบ</td>
<td width="27" align="center">เพิ่มเติม</td>
</tr>
<?php
while($rs=mysql_fetch_array($QUERY)) { ?>
<tr>
<td><?php echo $rs['product_id']; ?></td>
<td><?php echo $rs['product_name']; ?></td>
<td><?php echo $rs['cate_name']; ?></td>
<td><?php echo $rs['unit_name']; ?></td>
<td><?php echo $rs['product_balance']; ?></td>
<td><?php echo $rs['product_price']; ?></td>
<td><?php echo $rs['product_qty'];?></td>
<td><i class="icon-pencil"></i><a href="editproduct.php?product_id=<?php echo $rs['product_id']; ?>">แก้ไข</a></td>
<td align="center"><i class="icon-trash"></i><a onclick="return confirm_delete()" href="deleteproduct.php?product_id=<?php echo $rs['product_id'];?>">ลบ</a></td>
<td><i class="icon-asterisk"></i><a href="Purchase_detail.php?product_id=<?php
echo $rs['product_id']; ?>">เพิ่มเติม</a></td>
</tr>
<?php } $rs= mysql_fetch_assoc($Productset); ?>
</table>
<p>
หน้าที่:
<?php
for($dw_i=0;$dw_i<=$totalPages_Productset;$dw_i++){
echo '<a href="?pageNum_Productset=',$dw_i,'"class="pageNum">',($dw_i+1),'</a> ';
}
?>
</p>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
function confirm_delete() {
return confirm('ยืนยันการลบข้อมูล !');
}
</script>
<?php
mysql_free_result($Productset);
?>
Tag : PHP, MySQL, HTML/CSS, JavaScript, Ajax, jQuery
Date :
2016-03-26 23:10:42
By :
suwidha
View :
1679
Reply :
4
ตัวอย่าง
table stores สินค้าหลัก
id autoincrement
nm varchar 80
balance integer
table trans_in รายการสินค้าเข้า
id autoincrement
dte datetime
descript varchar 250
product_id FK stores.id
qty integer
table trans_out รายการสินค้าออก
id autoincrement
dte datetime
descript varchar 250
qty integer
นำเข้ารายละเอียดสินค้า
insert into stores ..............
ทำรายการสินค้าเข้า
insert into trans_in ................
ทำรายการสินค้าออก
Code (SQL)
set @pd_id := $product_id;
set @target := $sale_qty;
set @descript = '$descript';
insert into trans_out
select null, now(), @descript, @target
from (
select (sum(b) + sum(i) * sum(o)) bal from
(
select 0 b, qty i, 0 o from trans_in
where product_id=@pd_id
and (dte between @start_date and @end_date)
union all
select 0, qty from trans_out
where product_id=@pd_id
and (dte between @start_date and @end_date)
union all
select balance, 0, 0 from stores where id=@pd_id
) t1
) as t2 where bal>0
แสดงรายการยอดคงเหลือ
Code (SQL)
set @pd_id := $product_id;
select sum(bal) old, sum(buy) act_buy, sum(sale) act_sale from
(
select 0 bal, qty buy, 0 sale from trans_in where product_id=@pd_id union all
select 0 , 0 , qty from trans_out where product_id=@pd_id union all
select balance, 0, 0 from stores where id=@pd_id
) as t1
ตัดยอดครบดิว 1 3 6 หรือ 12 เดือน
Code (SQL)
set @start_date := '$start_date';
set @end_date := '$end_date';
update stores s inner join(
select product_id, sum(tin) t_in, sum(tout) t_out from
(
select product_id, qty tin, 0 tout from trans_in
where dte between @start_date and @end_date
union all
select 0, qty from trans_out
where dte between @start_date and @end_date
) t1 group by product_id
) t on t.product_id = s.id
set s.balance = s.balance + t_in - t_out
เสร็จแล้ว จะ backup จะ rename ก็ว่ากันไป เพื่อเริ่มการรับข้อมูลใหม่ ก็คิดเอานะครับ
ประวัติการแก้ไข 2016-03-27 08:02:59
Date :
2016-03-27 08:00:17
By :
Chaidhanan
รายละเอียดการซื้อ/ขาย หมายเหตุ์ หรืออะไรที่สำคัญสำหรับการอ้างอิง ก็ว่าไปครับ
Date :
2016-03-27 14:56:33
By :
Chaidhanan
Load balance : Server 02