我正在显示数据库中的一些设备(传感器)数据。数据包括devices_name
,如果有来自设备的警告,有关警告的信息,是否接受警告,谁接受等。我成功获取了 php 表上的所有信息。我想添加一个按钮来接受警告,以便按下按钮会将接受的警告更改为"is"。如何制作 - 按下按钮应该更改数据库值,我应该在我的 php 表中显示,然后接受按钮应该禁用。
<div>
<table class="dataTable">
<th >Date</th>
<th >Device Name</th>
<th >Warning Type/th>
<th >Warning acceted or not</th>
<th >Who accepted</th>
<th >Time of acceptance</th>
<th >Button to Accept</th>
</tr>
<?php
include ("DBconnect.php");
$conn= mysqli_connect( $dbhost, $dbuser, $dbpass, $db );
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM DeviceData ORDER BY Datum DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["Date"]. "</td>
<td>" . $row["Name"] . "</td>
<td>". $row["Warning"]. "</td>
<td>" . $row["AcceptOrNot"] . "</td>
<td>" . $row["AcceptTime"]."</td>
//a button to accept the warning
<td> <button class='btn' >Accept</button> </td>
</tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
</div>
我想要的是按下按钮应将其禁用,按下时间应显示在接受时间下,警告是否接受应更改为是,接受者将显示登录者的姓名。
我有所有这些信息的数据库表。 请帮忙。任何类型的信息...
最佳答案
您可以通过按钮 POST,然后在页面加载时处理 POST
会像这样工作:
//if the accept button was pressed
if(isset($_POST["accept"])) {
//change the AcceptOrNot value in the database
}
//get stuff from the database
//if it has not been accepted yet, put in the button
if(!$row["AcceptOrNot"]) {
echo '<form action="yourpage.php" method="post">
<input type="submit" name="accept" value="Accept" />
</form>';
}
或者,如果您不希望在按下按钮时再次加载页面,则可以使用 AJAX 来实现。
关于php - 如何向php表添加一次性选择按钮,并且选择应该更改按钮的值和数据库值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55703062/