I work on a simple webserver on a Raspberry Pi, displaying a MySQL table with two buttons : One (Switch) to inverse a boolean value, and one (Delete) to remove the entire row from the table.
What I do, basically, is a while loop on the MySQL table in which I : 1 : display the row and the buttons in a form, and 2 : check if the buttons have been clicked, and if so I send the query to the database and reload the page.
My problem is very simple : When I click on a “switch” button, the query is sent and the page is refreshed, and the value is correctly modified (the page display the value as it is in the database). But when I click on the other button, the “delete” one, after the page has been reloaded, the row still appears in the table. Only when I manually reload the page, the row disappear.
What I already tried : 1 : Check that the row is removed from the database BEFORE the manual refresh : DONE, and the row is correctly removed. 2 : Wait 500ms after the MySQL query, and then reload the page : DONE, and even when I wait 500ms, when the page reloads there is still the removed row.
Here is the whole code, the MySQL query is at the end :
<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>
Maybe someone saw this problem before ?
Advertisement
Answer
whell you are have problem with redirect. check this link to know more about redirect
Before you continue please backup your code.
To fix your problem you must move your <?php endwhile; ?>
after
</form> </tr>
then add input hidden after formname “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']; ?>">
and the last replace this:
<?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'); } } ?>
With this
<?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>'; } ?>
Im not say this is are the good solution. but it will solve your problem until you found the best solution.