簡體   English   中英

不能存儲會話值,使用while循環打印行

[英]can't store session value, using while loop to print row

我正在使用while循環按計划打印票證進行匹配。 我想在會話中存儲信息,但無法在while循環中使用

<?php
  session_start();
  $_SESSION['name']='set';
  $_SESSION['teama']=$team1;
  $_SESSION['teamb'] = $team2;

  $query = "SELECT team1, team2, time, date FROM wb_match"; 
  $result = $db->prepare($query);
  $result->execute(); 
  $result->bind_result($team1, $team2, $time, $date);
  while($result->fetch()) {
    echo $team1.' - '. $team2.' - '. $date.' - '. $time. '<form method="post" 
    action="selectseat.php"><input type="submit" value="Buy now" name="buy"/></form>';
  }
?>

產量

阿森納-巴爾頓-2014-03-15-18:00:00 [購買按鈕]

利物浦-埃弗頓-2014-03-17-10:00:00 [購買按鈕]

在下一頁上,我正在檢查會話繼續並打印

<?php  
  session_start();
  $one = $_SESSION['teama'];
  $two = $_SESSION['teamb'];    

  if(!isset($_SESSION['name'])){
    echo'not set';
  }
  else 
  if($_SESSION['name']=='set'){
    echo 'Session is set'. $team1;
    echo  $one, $two; 
  }
?>

打印會話已設置,僅此而已。

你應該做這個:

while($result->fetch()) {
  $_SESSION['name']  = true;
  $_SESSION['teama'] = $team1;
  $_SESSION['teamb'] = $team2;
  echo $team1.' - '. $team2.' - '. $date.' - '. $time. '<form method="post" 
  action="selectseat.php"><input type="submit" value="Buy now" name="buy"/></form>';
}

但是這樣做只會將最后一行存儲到會話中。 所以這是我的建議。

第一頁將顯示所有匹配項和按鈕的列表,例如:

// you might want to include the id that ids the match
$query = "SELECT id, team1, team2, time, date FROM wb_match"; 
$result = $db->prepare($query);
$result->execute(); 
$result->bind_result($matchId, $teamA, $teamB, $time, $date);
while($result->fetch()) {
    echo $teamA.' - '. $teamB.' - '. $date.' - '. $time. '
    <form method="post" action="selectseat.php">
       <input type="hidden" name="matchId" value="'. $matchId.'">
       <input type="submit" value="Buy now" name="buy"/>
    </form>';
}  

selectseat.php將收到發布的比賽ID,然后查詢該比賽的數據。

$matchId = $_POST["matchId"]

$query = "SELECT team1, team2, time, date FROM wb_match WHERE id = ?"; 
$result = $db->prepare($query);
$result->bind_param("i", $matchId);
$result->execute(); 
$result->bind_result($team1, $team2, $time, $date);

// do stuff to your result

讓我們分解一下

因為not set $_SESSION['name']您不會得到not set的輸出。

然后,由於$_SESSION['name'] == 'set'您將獲得Session is set $_SESSION['name'] == 'set' Session is set的輸出。

現在這是問題所在:

在您的首頁上進行設置

 $_SESSION['teama'] = $team1;
 $_SESSION['teamb'] = $team2;

但是此時$team1$team2 $team1都未定義。

只有在$result->fetch()才會填充這些變量。

填充它們后,您需要更新$_SESSION變量中的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM