首页 > 解决方案 > 我的会话一直发送一个固定值,而它应该是可变的

问题描述

好吧,我得到了这个脚本

while($row = $result->fetch_assoc()) {
        $_SESSION["sup"] = $row['id']; 
        if ($hi <  $row['buytime']) {

        } else {

           echo " <form action='Action1.php' method='get'>
                Thanks for buying from our shop , if the item with id <input type='submit' value='" . $row['id'] ."' class='btn-link'/>  </form>";

           echo "<form action='Action.php' method='post'>
                 is a bad tool , you can report by clicking on the following button <input  type='submit' value='Report Item' />
                 </form>";
        }
}

唯一的问题是 hat in line $_SESSION["sup"] = $row['id']; 不断发送一个固定值,它是 144 而 $row['id']; 不是固定值,我真的迷路了哈哈

如果会话是一个坏主意,我如何在不将 ID 插入隐藏输入的情况下将 ID 发送到 action.php?

更新两个页面

First PAge  



 <?php
 $conn = mysqli_connect("localhost", "localhost", "localhost", "localhost");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$buyer      = $_SESSION['username'];
$sql_shells = "SELECT buytime,id,situation,buyer FROM shells WHERE situation = 'sold' AND buyer = '$buyer'  ";
$dt = new DateTime();
$dt->modify('+172799 seconds');
$hi = $dt->format('Y-m-d H:i:s');
$_SESSION["sup"] = [];

$result = $conn->query($sql_shells);
if ($result->num_rows > 0) {

// output data of each row
while($row = $result->fetch_assoc()) {
 $_SESSION["sup"][] = $row['id'];
$_SESSION["sup"] = implode(',', $session['sup']);  // use this var

if ($hi <  $row['buytime']) {

         }else{

  echo " <form action='view_item.php' method='get'>
  Thanks for buying from our shop , if the item with id <input type='submit' value='" . $row['id'] ."' class='btn-link'/>  </form>";

  echo "<form action='reportinsert.php' method='post'>
  is a bad tool , you can report by clicking on the following button <input  type='submit' value='Report Item' />
       </form>";
 }
  }
  }else {

    }

    ?>

第二页

     <?php
   session_start();
  if (isset($_SESSION['username'])) {


 $link = mysqli_connect("localhost", "localhost", "localhost", "localhost");

 if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
 }

 $trn_date = date("Y-m-d H:i:s");

   $seller = $_SESSION['username']; 
 $id = $_POST['whatever'];
 $id = explode(',', $id); // You now have an array of IDs.
 $sql = "INSERT INTO reports (reporter,stats,trn_date,itemid) VALUES ('$seller','Opened','$trn_date','$id')";
 if(mysqli_query($link, $sql)){
 echo $id;
 echo $seller;
 } else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
 }

 } else
  {
header("location:/quickshops/login.php");
 }
 ?>

现在它给了我插入数组

标签: phphtml

解决方案


您将始终获得最后一行的值,原因是,您在每次迭代中设置会话,因此每当您计划在循环之外使用它时,您将获得最后一次迭代的值。

如果您想在会话中设置数组,您可以这样做:

$idsArray= array();
while($row = $result->fetch_assoc()) {
  $idsArray[] = $row['id']; 
}

$_SESSION["ids"]  =  $idsArray;

推荐阅读