Dear Sir:
I've wrotten two sects script to run a query, but the result is incorrect, the code I've listed below and the question code I've marked it, if you are free, please help me to resolve its. Thanks first!
The program is when I input a key in the first page and the result display in the second page; I clicked the PUR botton, the Item will be saved to a cookie. But when I click the botton at fourth(diffrents four PUR bottons), the cookie which saved query condition clause was lost and all items display in the page, which isn't we need and the program run innormal, on the other hand, the cookie which saves item record can only save three records, the first,the second and the lastest that you clicked. Could you help?
The codes:
//item_query.php
<?php
require 'functions.php';
delCookies();
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Language" content="zh-cn">
<title>Item Query</title>
</head>
<form method="POST" action="query_result.php">
<p><input type="radio" value="v1" name="r1" checked>Query From Item Code
<input type="text" name="prod_code" size="36"></font></p>
<p><input type="radio" value="v2" name="r1"> Query From Item Description
<input type="text" name="prod_des" size="36"></font></p>
<p align="center"><input type="submit" value=" Enter " name="B1">
<input type="reset" value=" Cancel " name="B2"></p>
</form>
</body>
//query_result.php
<?php
require 'functions.php';
$dblink=mysql_connect($DB_SERVER,$DB_LOGIN,$DB_PASSWORD);
if($r1==v1){
$tmp="%".$prod_code."%";
$tmp="WHERE ITEM_CODE LIKE '$tmp'";
setcookie("where_clause",$tmp);
}
if ($r1==v2) {
$tmp="%".$prod_des."%";
$tmp="WHERE DESCRIPTION LIKE '$tmp'";
setcookie("where_clause",$tmp);
}
//Attention:Above cookie where_clause will be lost at you clicked different PUR Botton.
if (trim($where_clause)=="") {
$s_sql="SELECT ITEM_NO,ITEM_CODE,DESCRIPTION,UOM,
CUR,PRICE FROM JP_ITEMS ".stripslashes($tmp);
}
else {
$s_sql="SELECT ITEM_NO,ITEM_CODE,DESCRIPTION,UOM,
CUR,PRICE FROM JP_ITEMS ".stripslashes($where_clause);
}
for ($i=0;$i<$total_items;$i++) {
if ($my_item[$i]==$h_item)
break;
}
if ($i==$total_items and trim($h_item)!="") {
setcookie("my_item[$total_items]",$h_item);
setcookie("my_des[$total_items]",$h_des);
setcookie("qty[$total_items] ",1);
setcookie("cur[$total_items]",$h_cur);
setcookie("my_price[$total_items]",$h_price);
$total_items++;
setcookie("total_items", $total_items);
//Attention: Above cookie can only save the first,the second and the lastest records.
}
echo "Total=".$total_items;
?>
<BODY bgcolor="#FFFFCC">
<center>
<TABLE Border=0 Cellspacing=5 bordercolor="#FFCC00">
<TR BgColor=#000099>
<TD><font color="#FFFFFF">Item Code</font></TD>
<TD><font color="#FFFFFF">Description</font></TD>
<TD NoWrapP><font color="#FFFFFF"> UOM </font></TD>
<TD><font color="#FFFFFF">Cur</font></TD>
<TD><font color="#FFFFFF">Price</font></TD>
<TD><font color="#FFFFFF">Purchase</font></TD>
</TR>
<?
$result=mysql_db_query($DB,$s_sql);
while($rows = mysql_fetch_array($result))
{
?>
<form action="query_pur.php" method="post">
<TR Valign=TOP>
<TD NoWrap>
<?echo $rows['ITEM_CODE'];?>
</TD>
<td nowrap>
<?echo $rows['DESCRIPTION'];?>
<TD nowrap>
<?echo $rows['UOM'];?>
</TD>
<TD Align=Right nowrap>
<?echo $rows['CUR'];?>
</TD>
<TD nowrap>
<?echo $rows['PRICE']?>
</TD>
<td>
<INPUT type="submit" value="PUR" name="button"> //which is the PUR Botton
</td>
<td>
<INPUT type="hidden" name="h_item" value="<?php echo $rows["ITEM_CODE"];?>">
</td>
<td>
<INPUT type="hidden" name="h_des" value="<?php echo $rows["DESCRIPTION"];?>">
</td>
<td>
<INPUT type="hidden" name="h_cur" value="<?php echo $rows["CUR"];?>">
</td>
<td>
<INPUT type="hidden" name="h_price" value="<?php echo $rows["PRICE"];?>">
</td>
</TR>
</form>
<?php
} // End while
?>
</TABLE>
<P>
<p><br>
<form method="POST" action="show_po.php">
<CENTER>
<INPUT type="submit" value="Your Order">
</CENTER>
</form>
</BODY>