 |
ช่วยดูโค๊ดให้หน่อยค่ะ บันทึกค่าส่่งสินค้าลงฐานข้อมูลเป็นลำดับไม่ได้ ค่ะ |
|
 |
|
|
 |
 |
|
บันทึกค่าส่่งสินค้าลงฐานข้อมูลเป็นลำดับไม่ได้ค่ะ คือ มันลงแต่ค่าลำดับล่าสุด
ค่าจัดส่งตรงที่ขีดเส้นไว้ค่ะ
ค่าส่ง 70 บาท/ชิ้น ถ้า 2 ชิ้นก็ 140 บาท แต่มันลง 140 ทั้ง 2 ชิ้นเลยค่ะ

โค๊ดหน้า a.php ส่งไปบันทึกหน้า b.php
<?PHP
session_start();
include"connect.php";
mysql_query("SET NAMES'utf-8'");
?>
<!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>ตะกร้าสินค้า</title>
<script language="JavaScript">
function OnSubmitForm()
{
if(document.pressed == 'ยืนยันการสั่งซื้อ>>')
{
document.form1.action ="addpro_con.php";
}
return true;
}
</script>
</head>
<body>
<form method="post" name="form1" class="style2" id="form1" onsubmit="return OnSubmitForm();">
<table width="847" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="108" align="center" bgcolor="#9ECD9E">รหัสสินค้า</td>
<td width="243" align="center" bgcolor="#9ECD9E">รายการ</td>
<td width="50" align="center" bgcolor="#9ECD9E">จำนวน</td>
<td width="74" align="center" bgcolor="#9ECD9E">ราคาขาย</td>
<td width="79" align="center" bgcolor="#9ECD9E">ราคารวม</td>
<td width="78" align="center" bgcolor="#9ECD9E">ค่าจัดส่ง</td>
</tr>
<?
$no=0;
if (isset($sess_basket)) {
foreach($sess_basket as $p_id=>$value){// foreach จนกว่าจะหมด ใส่ MaterialID จะเรียกใช้[ไปที่หน้า addpro_barket.php]
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><span class="style38"><? echo $p_id ?></span></td>
<td align="center" bgcolor="#FFFFFF"><span class="style38"><? echo $value["name"]?></span></td>
<td align="center" bgcolor="#FFFFFF"><span class="style38"><? echo $value["qty"]?></span></td>
<td align="center" bgcolor="#FFFFFF"><span class="style38">
<? echo number_format($value["price"],2)?></span></td>
<td align="center" bgcolor="#FFFFFF"><span class="style38">
<? echo number_format($value["price"]*$value["qty"],2)?></span></td>
<td align="center" bgcolor="#FFFFFF"><span class="style38">
<? echo $send_price=70*$value["qty"];?></span> </td>
</tr>
<?
$no+=1;
$send_price=70*$value["qty"];
} // end for
} // if (!isset($sess_basket))
?>
<input type="hidden" name="send_price" value="<?=$send_price?>"/><?=$send_price?>
<input type="submit" name="button2" onclick="document.pressed=this.value" value="ยืนยันการสั่งซื้อ>>" /></td>
</tr>
</table>
</form>
</body>
</html>
b.php
<?
@session_start();
include "connect.php";
include "function.php";
mysql_query("SET NAMES'utf-8'");
$p_id=$_REQUEST['p_id'];
echo $send_price=$_POST['send_price'];
$select_maxsale="select max(sale_id) sale_id from sale";
$result_maxsale=mysql_query($select_maxsale) or die(mysql_error());
$read_maxsale=mysql_fetch_array($result_maxsale);
$sale_id=substr($read_maxsale['sale_id'],1,4);
$sale_id+=1;
if($sale_id<10){
$sale_id="s000".$sale_id;
}
else if($sale_id>=10 and $sale_id<100){
$sale_id="s00".$sale_id;
}
else if($sale_id>=100 and $sale_id<1000){
$sale_id="s0".$sale_id;
}
else{
$sale_id="s".$sale_id;
}
$date=date("Y-m-d");
echo $insertsale="insert into sale (sale_id,sale_date,re_id,user_id,sale_sumtotal,sendprice_total,sale_status) values ('$sale_id','$date','$re_id','$sess_user_id','$total2','$send_price1','ชำระเงินแล้ว รอจัดส่ง')";
mysql_query($insertsale) or die(mysql_error());
$saledetail=1;
foreach($sess_basket as $p_id=>$value){
$selectprice="select * from product where p_id='$p_id' ";
$resultprice=mysql_query($selectprice);
while($readprice=mysql_fetch_array($resultprice)){
$price=$readprice['p_price'];
$qty=$value['qty'];
$total=$readprice['p_price']*$value['qty'];
echo $insertsale_detail="insert into sale_detail (sd_id,sale_id,p_id,sd_qty,sale_price,total,rr) values ('$saledetail','$sale_id','$p_id','$qty','$price','$total','$send_price')";
mysql_query($insertsale_detail) or die(mysql_error());
$saledetail++;
}
}
session_unregister("sess_barket");
session_unregister("sale_id");
echo "<script>alert('การสั่งซื้อเสร็จสมบูรณ์');window.location='bill_pro.php?sale_id=$sale_id'</script>";
?>
Tag : PHP, MySQL
|
|
 |
 |
 |
 |
Date :
2013-09-18 22:20:20 |
By :
jungnaka |
View :
642 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เห็น code แล้วตาลาย (มีบ่น)
ตอน insert sale_detail
ค่า $send_price น่าจะได้มาจาก
<input type="hidden" name="send_price" value="<?=$send_price?>"/><?=$send_price?>
ซึ่งมันจะได้ค่าสุดท้ายของตระกร้าสินค้า
ก่อน insert ก็ทำเหมือนตอนแสดงอ่ะครับ
$send_price=70*$value["qty"];
อีกนิด ค่า 70 ควรจะทำเป็น config หรือ เก็บใส่ base ไว้น่ะครับ เผื่อค่าส่งเปลี่่ยนจะได้ไม่ต้องมาแก้ code
|
 |
 |
 |
 |
Date :
2013-09-18 23:28:24 |
By :
ผ่านมา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
วาง $send_price=70*$value ไว้ตรงนี้หรือเปล่า ก็อบจากส่วนนี้มาค่ะ
foreach($sess_basket as $p_id=>$value){
$selectprice="select * from product where p_id='$p_id' ";
$resultprice=mysql_query($selectprice);
while($readprice=mysql_fetch_array($resultprice)){
$price=$readprice['p_price'];
$qty=$value['qty'];
$total=$readprice['p_price']*$value['qty'];
$send_price=70*$value["qty"]; <---
echo $insertsale_detail="insert into sale_detail (sd_id,sale_id,p_id,sd_qty,sale_price,total,rr) values ('$saledetail','$sale_id','$p_id','$qty','$price','$total','$send_price')";
mysql_query($insertsale_detail) or die(mysql_error());
|
 |
 |
 |
 |
Date :
2013-09-18 23:53:54 |
By :
jungnaka |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่า ๆ โอเคค่ะ ได้แล้ว ๆ ขอบคุณค่ะ 
|
 |
 |
 |
 |
Date :
2013-09-19 00:01:23 |
By :
jungnaka |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|