每当我使用浏览器后退按钮返回时,它都工作正常并将购物车存储在会话中。但是,当我使用重定向到index.php的锚标记回去时,会话丢失或创建了新数组。
<?php
session_start();
include_once 'connectiondb.php';
class dbcook {
public function Display_book()
{
$connect=new DBCONNECT();
$link=$connect->DBCONNECTION();
$disq_qry="select * from tbbook";
$dis_result=mysqli_query($link,$disq_qry);
$i=1;
$output[]='<form name=abc action=index.php method=POST >';
$output[]="<table border=5>";
while($dis_row=mysqli_fetch_row($dis_result))
{
$i++;
if($i==2)
{
$output[]='<tr>';
$i=0;
}
$output[]="<td>";
$output[]="<img src=$dis_row[5] width=50 />";
$output[]="</td>";
$output[]='<td>';
$output[]="Title:<a href=details.php?bid=$dis_row[0] >$dis_row[1]</a><br>";
$output[]="Author:$dis_row[2]<br>";
$output[]="Publisher:$dis_row[3]<br>";
$output[]="Price:$dis_row[4]";
$output[]="<input type=checkbox name=add[] value=$dis_row[0] />";
$output[]='</td>';
if($i==1)
{
$output[]='</tr>';
}
}
$output[]='<tr>';
$output[]='<td>';
$output[]='<input type="submit" name="btnsubmit" value="Add" />';
$output[]='</td>';
$output[]='</tr>';
$output[]='</table>';
$output[]='</form>';
echo join($output);
if(isset($_POST["btnsubmit"]))
{
// session_start();
$add=$_POST["add"];
foreach ($add as $value )
{
// if(isset($bid))
// $_SESSION["bid"]=$value;
$bid[]=$value;
}
$_SESSION["bid"]=$bid;
//print_r($_SESSION["bid"]);
header('location: bookcart.php?action=Add');
// echo$_SESSION["bid"];
}
}
public function Cart_Add()
{
$connect=new DBCONNECT();
$connect->DBCONNECTION();
if(isset($_SESSION["cart"]))
$cart=$_SESSION["cart"];
if(isset($_SESSION["bid"]))
$bid=$_SESSION["bid"];
if(isset($_REQUEST["action"]))
$action=$_REQUEST["action"];
switch ($action)
{
case 'Add':
if(isset($cart))
{
$cart=$bid;
//echo"if work";
}
else
{
$cart=$bid;
// echo"else work";
}
break;
}
$_SESSION["cart"]=$cart;
// print_r($_SESSION["cart"]);
}
public function Cart_detail()
{
// session_start();
$connect=new DBCONNECT();
$link=$connect->DBCONNECTION();
$totalamt=0;
if(isset($_SESSION["cart"]))
{
if($_SESSION["cart"]!="")
{
$arraybid=$_SESSION["cart"];
$d= implode(",",$arraybid);
$items= explode(',',$d);
// echo $bid;
foreach ($items as $item)
{
$contents[$item]= isset($contents[$item])?$contents[$item]+1:1;
}
$out[]="<form name=abc action=bookcart.php?action=Update method=POST>";
$out[]='<table border=2>';
$out[]='<tr><th></th><th>Book Detail</th><th>Publisher</th><th>Price</th><th>Qty</th><th>Amount</th></tr>';
foreach ($contents as $bid=>$qty)
{
$qry="select * from tbbook where bid in($bid)";
$res=mysqli_query($link,$qry) or die('Query_Error');
if($row= mysqli_fetch_row($res))
{
$out[]='<tr>';
$out[]='<td>';
$out[]="<a href=bookcart.php?action=Delete>Delete</a>";
$out[]='</td>';
$out[]='<td>';
$out[]="$row[1] by $$row[2]";
$out[]='</td>';
$out[]='<td>';
$out[]="$row[3]";
$out[]='</td>';
$out[]='<td>';
$out[]="$row[4]";
$out[]='</td>';
$out[]='<td>';
$out[]="<input type=text name=qty$row[0] value=$qty placeholder=QTY/>";
$out[]='</td>';
$out[]='<td>';
$amt=$row[4]*$qty;
$out[]=$amt;
$out[]='</td>';
$totalamt+=$amt;
$out[]='</tr>';
}
}
$out[]='<tr>';
$out[]='<td>';
$out[]="Totalamt:$totalamt";
$out[]='</td>';
$out[]='<td>';
$out[]="<a href='index.php'>Go Back</a>";
$out[]='</td>';
$out[]='</tr>';
$_SESSION["totalamt"]=$totalamt;
$out[]='</table>';
$out[]="<input type=submit name=btnsubmit value=Update />";
$out[]='</form>';
$out[]="<a href=pay.php>Pay Now</a>";
echo join($out);
}
}
}
}
?>
运行此命令后,我无法在此会话中存储购物车。通过浏览器的后退按钮进行导航时,我能够存储购物车,但无法通过锚标记进行存储。
答案 0 :(得分:0)
在index.php文件中是否有session_start()
?