PHP/MYSQL: 删除的行在重新加载后仍显示在我的页面中
PHP/MYSQL: A removed row is still displayed in my page after a reload
我在 Raspberry Pi 上的一个简单网络服务器上工作,显示 MySQL table 有两个按钮:一个(切换)反转布尔值,一个(删除)从 table 中删除整行。
基本上,我所做的是 MySQL table 上的 while 循环,其中我:1:在表单中显示行和按钮,以及 2:检查按钮是否已被点击,如果是,我将查询发送到数据库并重新加载页面。
我的问题很简单:当我点击 "switch" 按钮时,查询被发送,页面被刷新,值被正确修改(页面显示值,因为它在数据库)。但是,当我单击另一个按钮 "delete" 时,页面重新加载后,该行仍出现在 table 中。只有当我手动重新加载页面时,该行才会消失。
我已经尝试过的:
1:在手动刷新之前检查该行是否已从数据库中删除:完成,并且该行已正确删除。
2:在 MySQL 查询后等待 500 毫秒,然后重新加载页面:完成,即使我等待 500 毫秒,当页面重新加载时仍然有删除的行。
这是完整的代码,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>
也许有人以前见过这个问题?
只需尝试在删除查询之后但在重定向之前清理结果数组,方法是:unset($row);
并通过启动清理 $result
:$result='';
修改代码如下,在上面两行和下面两行添加四行注释:
<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);
}
$row=array(); //Initialize the empty array (By shashi).
$result=''; //Initialize the empty result var (By shashi).
$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);
unset($row); //Clear the array (By shashi).
result=''; //Clear result Var (By shashi).
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>
$sql="SELECT * FROM $table;"
应该在
之后
$sql3="DELETE FROM $table WHERE id=".$row['id'].";";
以及更新等其他操作...
看起来它已被删除,但您看到的是旧数据:)
<?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);
}
if($_REQUEST['Delete']) {
{
$sql3="DELETE FROM $table WHERE id=".(int)$_REQUEST['Delete'].";";
$result3=$conn->query($sql3);
}
}
if($_REQUEST['Switch']) {
{
$sql2="UPDATE $table SET state= IF (`state`, 0, 1) WHERE id=".(int)$_REQUEST['Switch'].";";
$result2=$conn->query($sql2);
}
}
$sql="SELECT * FROM $table;";
$result=$conn->query($sql); ?>
<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>
<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='".$row['id']."'>"; ?></td>
<td><?php echo "<input name='Delete' type='submit' id='Delete' value='".$row['id']."'>"; ?></td>
</form>
</tr>
<?php endwhile; ?>
</table>
</table>
</td>
</tr>
</table>
请不要在这种情况下在 WHILE 循环中使用某些 SQL 查询
删除后,您指示浏览器重新加载页面:
header('Location: table.php');
但是,您无法在开始发送数据后发送 HTTP headers。您可能会在 table 下方某处看到一条错误消息,指出 "headers already sent".
您需要将页面内容的输出移动到上次使用 header()
函数修改响应 headers 之后,或者使用 output buffering.
您遇到重定向问题。检查此 link 以了解有关 redirect
的更多信息
在继续之前,请备份您的代码。
要解决您的问题,您必须将 <?php endwhile; ?>
移到
之后
</form>
</tr>
然后在表单名称后添加隐藏的输入"Delete"
<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>
<input name='id' type='hidden' value="<?php echo $row['id']; ?>">
<input name='state' type='hidden' value="<?php echo $row['state']; ?>">
最后一个替换为:
<?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
if (!empty($_REQUEST['Delete']) && !empty($_REQUEST['id']))
{
$sql3="DELETE FROM $table WHERE id=".$_REQUEST['id'].";";
$result3=$conn->query($sql3);
echo '<script> location.replace("index.php"); </script>';
}
if (!empty($_REQUEST['Switch']) && !empty($_REQUEST['id']) && isset($_REQUEST['state']))
{
var_dump($_REQUEST['Switch']);
if($_REQUEST['state'] == '1')
{
$sql2="UPDATE $table SET state='0' WHERE id=".$_REQUEST['id'].";";
}
else
{
$sql2="UPDATE $table SET state='1' WHERE id=".$_REQUEST['id'].";";
}
$result2=$conn->query($sql2);
echo '<script> location.replace("index.php"); </script>';
}
?>
我并不是说这是好的解决方案。但它会解决您的问题,直到您找到最佳解决方案。
这是一个很简单的问题,因为我遇到过他们,问题是SQL语句,你需要看看MYSQL,所以我解决了这个例子:
DELETE FROM `reyler` WHERE `reyler`.`id`=".$id_post1 ."";
你需要这个符号 => `` 它不是这个 " 或这个 '
其次你可以从 mysql 和 mysql 中删除,然后再删除 window 和 sql 语句,你可以从那里复制正确的语句,这与你的关联您的主机或 xampp 服务器中的数据库,我认为会对您有所帮助。))
我在 Raspberry Pi 上的一个简单网络服务器上工作,显示 MySQL table 有两个按钮:一个(切换)反转布尔值,一个(删除)从 table 中删除整行。
基本上,我所做的是 MySQL table 上的 while 循环,其中我:1:在表单中显示行和按钮,以及 2:检查按钮是否已被点击,如果是,我将查询发送到数据库并重新加载页面。
我的问题很简单:当我点击 "switch" 按钮时,查询被发送,页面被刷新,值被正确修改(页面显示值,因为它在数据库)。但是,当我单击另一个按钮 "delete" 时,页面重新加载后,该行仍出现在 table 中。只有当我手动重新加载页面时,该行才会消失。
我已经尝试过的: 1:在手动刷新之前检查该行是否已从数据库中删除:完成,并且该行已正确删除。 2:在 MySQL 查询后等待 500 毫秒,然后重新加载页面:完成,即使我等待 500 毫秒,当页面重新加载时仍然有删除的行。
这是完整的代码,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>
也许有人以前见过这个问题?
只需尝试在删除查询之后但在重定向之前清理结果数组,方法是:unset($row);
并通过启动清理 $result
:$result='';
修改代码如下,在上面两行和下面两行添加四行注释:
<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);
}
$row=array(); //Initialize the empty array (By shashi).
$result=''; //Initialize the empty result var (By shashi).
$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);
unset($row); //Clear the array (By shashi).
result=''; //Clear result Var (By shashi).
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>
$sql="SELECT * FROM $table;"
应该在
之后$sql3="DELETE FROM $table WHERE id=".$row['id'].";";
以及更新等其他操作...
看起来它已被删除,但您看到的是旧数据:)
<?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);
}
if($_REQUEST['Delete']) {
{
$sql3="DELETE FROM $table WHERE id=".(int)$_REQUEST['Delete'].";";
$result3=$conn->query($sql3);
}
}
if($_REQUEST['Switch']) {
{
$sql2="UPDATE $table SET state= IF (`state`, 0, 1) WHERE id=".(int)$_REQUEST['Switch'].";";
$result2=$conn->query($sql2);
}
}
$sql="SELECT * FROM $table;";
$result=$conn->query($sql); ?>
<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>
<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='".$row['id']."'>"; ?></td>
<td><?php echo "<input name='Delete' type='submit' id='Delete' value='".$row['id']."'>"; ?></td>
</form>
</tr>
<?php endwhile; ?>
</table>
</table>
</td>
</tr>
</table>
请不要在这种情况下在 WHILE 循环中使用某些 SQL 查询
删除后,您指示浏览器重新加载页面:
header('Location: table.php');
但是,您无法在开始发送数据后发送 HTTP headers。您可能会在 table 下方某处看到一条错误消息,指出 "headers already sent".
您需要将页面内容的输出移动到上次使用 header()
函数修改响应 headers 之后,或者使用 output buffering.
您遇到重定向问题。检查此 link 以了解有关 redirect
的更多信息在继续之前,请备份您的代码。
要解决您的问题,您必须将 <?php endwhile; ?>
移到
</form>
</tr>
然后在表单名称后添加隐藏的输入"Delete"
<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>
<input name='id' type='hidden' value="<?php echo $row['id']; ?>">
<input name='state' type='hidden' value="<?php echo $row['state']; ?>">
最后一个替换为:
<?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
if (!empty($_REQUEST['Delete']) && !empty($_REQUEST['id']))
{
$sql3="DELETE FROM $table WHERE id=".$_REQUEST['id'].";";
$result3=$conn->query($sql3);
echo '<script> location.replace("index.php"); </script>';
}
if (!empty($_REQUEST['Switch']) && !empty($_REQUEST['id']) && isset($_REQUEST['state']))
{
var_dump($_REQUEST['Switch']);
if($_REQUEST['state'] == '1')
{
$sql2="UPDATE $table SET state='0' WHERE id=".$_REQUEST['id'].";";
}
else
{
$sql2="UPDATE $table SET state='1' WHERE id=".$_REQUEST['id'].";";
}
$result2=$conn->query($sql2);
echo '<script> location.replace("index.php"); </script>';
}
?>
我并不是说这是好的解决方案。但它会解决您的问题,直到您找到最佳解决方案。
这是一个很简单的问题,因为我遇到过他们,问题是SQL语句,你需要看看MYSQL,所以我解决了这个例子:
DELETE FROM `reyler` WHERE `reyler`.`id`=".$id_post1 ."";
你需要这个符号 => `` 它不是这个 " 或这个 '
其次你可以从 mysql 和 mysql 中删除,然后再删除 window 和 sql 语句,你可以从那里复制正确的语句,这与你的关联您的主机或 xampp 服务器中的数据库,我认为会对您有所帮助。))