我的会话会不断发送固定值,而该值应该是可变的

时间:2018-06-25 07:50:59

标签: php html

我有这个脚本

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>";
        }
}

唯一的问题是$_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");
 }
 ?>

现在它给我插入的数组

3 个答案:

答案 0 :(得分:0)

每次循环时,您都在重写值:

 // loop through results
 while($row = $result->fetch_assoc()) {
        // overwrite session variable with the last id
        $_SESSION["sup"] = $row['id']; 

因此,如果您有10个结果,则会话var将是第10行的ID。

如果没有更多关于您要通过结果集尝试实现的目标的详细信息,我们真的无能为力。如果这还不足以解决您的问题,也许可以用更多详细信息更新您的问题?

  

更新好,所以您需要所有ID

您可以将会话var设置为一个数组,并且当需要发布ID时,请用逗号implode()来代替。然后在接收脚本中,以逗号爆炸以获取所有ID。

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

在其他脚本中

$ids = $_POST['whatever'];
$ids = explode(',', $ids); // You now have an array of IDs.

顺便说一句,由于您使用的是会话变量,您可能甚至不需要发布它,而只需使用其他脚本访问该会话,但是我不知道您的代码在做什么

答案 1 :(得分:0)

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

如果要在会话中设置数组,可以采用以下方法:

<%
    if(request.getParameter("level")!=null){
    int level = Integer.parseInt(request.getParameter("level"));
    String Process = request.getParameter("Process");
    AlertsHelper helper = new AlertsHelper();
    List alertList = helper.getAlertsList(level, Process);
    out.print("<table border='1'>");
    out.print("<tr><th>Level</th><th>Process</th></tr>");
    for(int i = 0; i <alertList.size(); i++){
    Dtalerts alerts = (Dtalerts) alertList.get(i);
    Id = alerts.getId();
    level = alerts.getLevel();
    Process = alerts.getProcess();
    String Description = alerts.getDescription();
    out.print("<td><a href='javascript:mostrarDatos(" + level + ",\"" + Process + ")'>Seleccionar</a></td>");
    out.print("<tr>");
    out.print("<td>" + level + "</td>");
    out.print("<td>" + Process + "</td>");
    out.print("<td>" + Description + "</td>");
    out.print("<td>" + Id + "</td>");
    out.print("</tr>");
    }
    out.print("</table>");
    }else{
    out.print("introduce un id");
    }%>
    </form>

答案 2 :(得分:0)

从$ row ['id']中删除$ row”。