|
 |
|
จะเอาค่า ที่แสดงแบบ while loop ไปแล้ว ไป insert แบบวน loop ยังไงหรอครับ |
|
 |
|
|
 |
 |
|
Code (PHP)
while($row=mysql_fetch_assoc($result))
{
echo '<h2>'.$row['Pname'].' '.$cnt[$row['Pid']].' ชุด '.$cnt[$row['Pid']]*$row['Price'].' ฿</h2>';
$total+=$cnt[$row['Pid']]*$row['Price'];
$strSQL = " INSERT INTO table ....";
mysql_query($strSQL);
}
ลองแบบนี้ดูครับ
|
 |
 |
 |
 |
Date :
2013-02-14 06:28:13 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2013-02-14 06:28:13
รายละเอียดของการตอบ ::
ขอบคุณครับ อ่อ insert ใน loop เลย insert ได้ครับ
แล้วถ้า แบบมีเงื่อนไขหละครับ แบบ submit ถึง เก็บค่าแล้ว ส่งค่าไป ค่อย insert ได้รึเปล่าครับ หรือไม่ ก็
แล้ว พอ เคลียร์ ก็ลบ order detail ที่ตอนinsert ใน loop
if(isset($_POST['Clear'])){
$Del = "delete from order_detail where productid=".$row['Pid'];
$qry = mysql_query($Del);

Code (PHP)
<?php
session_start();
$ID =$_SESSION["UserID"];
$Name = $_SESSION["username"];
require("config.inc.php");
if(!$_POST)
{
if($_SERVER['HTTP_REFERER'])
header('Location : '.$_SERVER['HTTP_REFERER']);
exit;
}
?>
<!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>Checkout! | Jaidog</title>
<link rel="stylesheet" type="text/css" href="demo.css" />
<script type="text/javascript" src="js/jquery1_3_2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
<script type="text/javascript" src="simpletip/jquery.simpletip-1.3.1.pack.js"></script>
<script type="text/javascript" src="script.js"></script>
<style type="text/css">
body {
background-image: url(images/DSC04830.JPG);
}
#apDiv1 {
position:absolute;
left:779px;
top:93px;
width:318px;
height:86px;
z-index:1;
}
</style>
</head>
<body>
<div id="main-container">
<div class="container"> <span class="top-label"> <span class="label-txt">Your order / คุณสั่ง</span> </span>
<div class="content-area">
<div class="content">
<?php
$cnt = array();
$products = array();
foreach($_POST as $key=>$value)
{
$key=(int)str_replace('_cnt','',$key);
$products[]=$key;
$cnt[$key]=$value;
}
$result = mysql_query("SELECT * FROM product WHERE Pid IN(".join($products,',').")");
if(!mysql_num_rows($result))
{
echo '<h1>There was an error with your order!</h1>';
}
else
{
echo "วันที่สั่ง ". date ( "l / j / F / Y" );
echo '<h1>'.$Name.' You ordered:</h1>';
while($row=mysql_fetch_assoc($result))
{
echo '<h2>'.$row['Pname'].' '.$cnt[$row['Pid']].' ชุด '.$cnt[$row['Pid']]*$row['Price'].' ฿</h2>';
$total+=$cnt[$row['Pid']]*$row['Price'];
$strSQL = "INSERT INTO order_detail VALUES(0,'".$row['Pid']."','".$row['Pname']."','".$row['Price']."','".$cnt[$row['Pid']]."')";
mysql_query($strSQL) or die(mysql_error());
//echo "<center><font color=#0000FF size=6>บันทึกการสั่งอาหาร สำเร็จ</font></center>";
//echo "<meta http-equiv='refresh' content='3;URL=index.php'>";
if(isset($_POST['Clear'])){
$Del = "delete from order_detail where productid=".$row['Pid'];
$qry = mysql_query($Del);
}
}
}
echo '<h1>รวมเป็นเงิน: '.$total.'฿</h1>';
$sqluserID = "select username, name, address, phone from userinfo where username='".$Name."'";
$qry = mysql_query($sqluserID);
$UserRS = mysql_fetch_array($qry);
?>
<form method="post" action="<? $_SERVER['PHP_SELF']; ?>" >
<table width="328" height="119">
<tr>
<td width="108">คุณ :</td>
<td width="208"><input name="name" type="text" readonly value="<?=$UserRS['name']; ?>"/></td>
</tr>
<tr>
<td>ที่อยู่ในการจัดส่ง :</td>
<td><textarea name="address" cols="30" rows="4" readonly><?=$UserRS['address']; ?></textarea></td>
</tr>
<tr>
<td>หมายเลขติดต่อ :</td>
<td><input name="phone" type="text" readonly value="<?=$UserRS['phone']; ?>"/></td>
</tr>
</table>
<center>
<input type="submit" name="confirm" id="confirm" size="10" value="confirm" />
<a href="MainCart.php" >
<input type="button" name="Clear" id="Clear" value="Clear" />
</a>
</center>
</form>
<div class="clear"></div>
</div>
</div>
<div class="bottom-container-border"> </div>
</div>
</div>
<div id="apDiv1">
<?
if(isset($_POST['confirm'])){
$sql = "INSERT INTO orders Values(0,'".$_POST["name"]."','".$_POST["address"]."' ,'".$_POST["phone"]."', '".date("Y-m-d H:i:s")."')";
$qry = mysql_query($sql) or die(mysql_error());
//เอาค่าใน loop มา insert ตรงนี้ได้ไหมครับ
if(!$qry){
echo "<li>" . mysql_error();
}
else{
$Uid = mysql_insert_id();
echo "<center>บันทึกการสั่งอาหาร สำเร็จ</center>";
}
}
?>
</div>
</body>
</html>
|
ประวัติการแก้ไข 2013-02-14 18:12:51 2013-02-14 18:14:12 2013-02-14 18:15:23
 |
 |
 |
 |
Date :
2013-02-14 18:07:33 |
By :
kanokpong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|