php - 我的会话一直发送一个固定值,而它应该是可变的
问题描述
好吧,我得到了这个脚本
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");
}
?>
现在它给了我插入数组
解决方案
您将始终获得最后一行的值,原因是,您在每次迭代中设置会话,因此每当您计划在循环之外使用它时,您将获得最后一次迭代的值。
如果您想在会话中设置数组,您可以这样做:
$idsArray= array();
while($row = $result->fetch_assoc()) {
$idsArray[] = $row['id'];
}
$_SESSION["ids"] = $idsArray;
推荐阅读
- python - 我使用 Flask 创建了一个网站,并且有两个 html 页面如何连接它们?
- spring - Spring WebFluxTest 禁用 AWS 自动配置
- javascript - javascript加载后是否可以从页面中以纯文本形式获取特定div中的HTML?
- python - 从带有异常的异步任务列表中收集结果
- django - 无法理解 Django REST 框架 JWT Auth
- security - 创建负载平衡器后如何关闭 Google App Engine URL 以进行直接访问
- sharepoint - 在 Teams 中显示来自另一个租户 SharePoint Online 的页面。我想使用自定义 iFrame。是否可以通过对用户进行身份验证
- android - 如何为二进制可执行文件创建 APK 文件?
- string - Ada 自定义图像
- sql-server - 将 tempdb 数据库复制到更永久的数据库