【问题标题】:Two php forms. second form gets data from first. but refreshing second page shows error两种php形式。第二种形式从第一种获取数据。但刷新第二页显示错误
【发布时间】:2019-04-23 11:07:46
【问题描述】:

我正在显示来自数据库的设备信息列表。每个设备都有一个“显示警告”按钮。用户可以单击“显示警告”,我正在使用隐藏字段(隐藏字段中的值来识别)来显示设备警告消息。来自数据库的每个警告都有一个接受按钮(一个带有接受按钮的警告列表)。用户可以点击接受按钮,这样警告的状态就会从不接受变为接受(并且页面会刷新)。问题在于刷新,因为它没有来自第一页的信息,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 不再存在。因此更新后不会显示任何警告消息。怎么改???任何帮助

【问题讨论】:

  • 对于您当前的问题,请详细说明php returns error。那是什么错误?有关其他说明....您应该查看一些最近编写的教程。你在这里有很多问题。您对 SQL 注入持开放态度。你不需要echo 每一行。您不需要连接每个元素。您的 HTML 属性值应该用引号括起来。
  • 嗨.. 我知道你所有的 cmets。谢谢你。一旦它起作用,我将改变一切。这里没有特别的错误。但提交第二页将返回“0 个结果”,因为 $deviceID 不再存在
  • 您的input 字段无效。也许浏览器正在纠正它们而不是在发送时传输它们? "&lt;td &gt;"." &lt;input type=submit name=acceptID value=ShowAlarm"." &lt;/td&gt;" 应该是 '&lt;td&gt;&lt;input type="submit" name="acceptID" value="ShowAlarm" /&gt;&lt;/td&gt;'

标签: php mysql database forms


【解决方案1】:

您的second page 仅在&lt;form action = alarms_list_display.php method =post&gt;"; 被提交后才获得价值,这就是为什么您在刷新第二页时会出现错误,因为它会丢失$_POST 数组

解决方案

您可以将&lt;form action = alarms_list_display.php method =post&gt;" 的操作设置为同一页面并将值存储在会话中,然后将其重定向到第二页,然后您可以使用会话中的值

【讨论】:

  • 哦,是的……应该可以了……我会试试的……非常感谢
  • 法罗克。我正要应用这种技术。我已经在使用一个会话来存储登录详细信息(用户详细信息)。可以使用多个会话吗?有安全隐患吗?
  • 我认为这不安全。会话存储在服务器中。一次使用多个会话是可以的。但您必须确保您使用的是最近更新的会话
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-15
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
相关资源
最近更新 更多