我正在显示数据库中的设备信息列表。每个设备都会有一个“显示警告”按钮。用户可以单击“显示警告”,我正在使用隐藏字段(来自隐藏字段的值来标识)来显示设备警告消息。数据库中的每个警告都有一个接受按钮(警告列表,每个警告都有接受按钮)。用户可以单击接受按钮,以使警告状态从未接受变为接受(并且页面将刷新)。问题在于刷新,因为它没有首页上的信息,因此php返回错误。
//page-1.. under each category (like electrical, mechanical, more than one types of devices
//device can be identified by using ID
echo '<tr><th>Category Name</th>';
echo '<td >' . $row['category']. '</td></tr>';
echo '<tr><th>Device ID</th>';
echo '<td >' . $row['ID']. '</td></tr>';
//form section.
echo "<form action = alarms_list_display.php method =post>";
echo '<tr>';
echo "<td >"." <input type=submit name=acceptID value=ShowAlarm"." </td>";
echo "<td >"." <input type=hidden name=hiddenID value =". $row["ID"]." </td>";
echo "</tr>";
echo "</form>";
//second page.. alarms_list_display.php
include ("DBconnect.php");
$conn= mysqli_connect( $dbhost, $dbuser, $dbpass, $db );
if(isset($_POST ['acceptID']))
{$deviceID = $_POST['hiddenID'];
}
else {$deviceID='';}
//above code is problematic. as it is available only when i submit the first page.
//when updating the second page (and refreshing), $deviceID is not available anymore.
//listing all warnings related to that device when user clicked the 'ShowAlarm' button.
if ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);}
$sql = "SELECT * FROM dataTable WHERE ID='$deviceID' ";
$result = $conn->query($sql);
//form with buttons to accept warning
//skipped table heading parts
//each warning will have a serialNumber. Using to accept the warning and update the database
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<form action = alarms_list_display_copy.php method =get>";
echo "<td>" . $row["Date"]. "</td>";
echo "<td>" . $row["Warning"]. "</td>";
echo "<td>"." <input type=hidden name=hidden value =" .$row["SerialNumber"]." </td>";
echo "<td>"." <input type=submit name=accept value=AcceptAlarm id=button1 class = formclass"." </td>";
echo "</tr>";
echo "</form>";
} else { echo "0 results"; }
//code to update when clicking the AcceptAlarm button.
if (isset($_GET['accept'])){
$query= "SELECT WarningAccept FROM dataTable WHERE SerialNumber= '$_GET[hidden]' ";
$result = mysqli_query($conn, $query);
while($row = $result->fetch_assoc()) {
$updateQuery = " UPDATE dataTable SET WarningAccept=1 WHERE SerialNumber= '$_GET[hidden]' ";
mysqli_query($conn, $updateQuery);
}
可以提交第一页。它将显示与该设备相对应的所有警告。但是当接受警告时,页面将刷新并且$ deviceID不再存在。因此更新后不会显示任何警告消息。如何改变它???任何帮助
答案 0 :(得分:0)
您的second page
仅在提交<form action = alarms_list_display.php method =post>";
后才获得价值,这就是刷新第二页时会出错的原因,因为第二页会丢失$_POST
数组,然后
解决方案
您可以将<form action = alarms_list_display.php method =post>"
的操作设置到同一页面并将值存储在会话中,然后将其重定向到第二页,然后可以使用会话中的值