【发布时间】:2015-04-17 11:26:39
【问题描述】:
我在 Raspberry Pi 上的一个简单网络服务器上工作,显示一个带有两个按钮的 MySQL 表:一个(切换)用于反转布尔值,一个(删除)用于从表中删除整行。
基本上,我所做的是在 MySQL 表上进行 while 循环,其中我:1:在表单中显示行和按钮,2:检查按钮是否被点击,如果是,我发送查询数据库并重新加载页面。
我的问题很简单:当我单击“切换”按钮时,发送查询并刷新页面,并且正确修改了值(页面显示数据库中的值)。但是当我点击另一个按钮时,“删除”一个,在页面重新加载后,该行仍然出现在表格中。只有当我手动重新加载页面时,该行才会消失。
我已经尝试过的: 1:在手动刷新之前检查该行是否已从数据库中删除:DONE,并且该行已正确删除。 2:MySQL查询后等待500ms,然后重新加载页面:DONE,即使我等待500ms,当页面重新加载时仍然有删除的行。
这是整个代码,MySQL查询在最后:
<form action="logout.php" method="post">
<input type="submit" name="logout" value="logout">
</form>
<b>Alarm Monitoring</b>
<p>Sensors state table. Clicking on "Switch state" will switch the field "state" of the associated mote. </p>
<p>State 1 means that the mote is active, the alarm will be activated if the mote is moved or if someone is detected.</p>
<p>State 0 means that the mote is inactive, the alarm will NOT be activated if the mote is moved or if someone is detected.</p>
<?php
$servername="localhost";
$username="root";
$password="patate";
$dbname="testbase";
$table="matable";
$conn = new mysqli($servername,$username,$password,$dbname);
if ($conn->connect_error) {
echo "kaput";
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT * FROM $table;";
$result=$conn->query($sql); ?>
<table border=0>
<tr>
<td>
<table border=1>
<tr>
<td width=150>ID</td>
<td width=150>Date</td>
<td width=300>PIR</td>
<td width=150>Accel</td>
<td width=100>State</td>
<td width=150>Switch</td>
<td width=150>Delete</td>
</tr>
<?php
while($row = $result->fetch_assoc()) : ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['pir']; ?></td>
<td><?php echo $row['accel']; ?></td>
<td><?php echo $row['state']; ?></td>
<form name="form1" method="post" action=''>
<td><?php echo "<input name='Switch' type='submit' id='Switch' value='Switch state ".$row['id']."'>"; ?></td>
<td><?php echo "<input name='Delete' type='submit' id='Delete' value='Delete mote ".$row['id']."'>"; ?></td>
</form>
</tr>
<?php
if($_REQUEST['Delete']=='Delete mote ' . $row['id']) {
{
$sql3="DELETE FROM $table WHERE id=".$row['id'].";";
$result3=$conn->query($sql3);
header('Location: table.php');
}
}
if($_REQUEST['Switch']=='Switch state ' . $row['id']) {
{
if($row['state']=='1')
{
$sql2="UPDATE $table SET state='0' WHERE id=".$row['id'].";";
}
else
{
$sql2="UPDATE $table SET state='1' WHERE id=".$row['id'].";";
}
$result2=$conn->query($sql2);
header('Location: table.php');
}
}
?>
<?php endwhile; ?>
</table>
</table>
</td>
</tr>
</table>
也许有人以前见过这个问题?
【问题讨论】:
-
添加查询错误处理:
if (!$mysqli->query($sql)) { printf("Errormessage: %s\n", $mysqli->error); } -
我添加并测试了,但没有显示错误。无论如何,当我单击时,该行已从表中正确删除,我想 MySQL 部分没有错误。
-
只是在空中抛出另一个想法,它可能是浏览器缓存吗?
-
可能是浏览器缓存,我想过,但我该怎么办?我可以在代码中清除它,还是强制重新加载?
标签: php html mysql apache2 raspberry-pi