使用php页面从数据库中删除SQL数据


Delete SQL data from database with php page

我正在制作一个应用程序来保护你家的门窗。该应用程序完成了80%,但我不知道如何删除一扇门的数据库记录。问题是我找不到一种方法来读出正确的数据来删除记录。你能看看我为删除页写的剧本吗?

我有一个页面可以在表格中插入所有数据,以概述所有"连接"到应用程序的设备。

<section>
        <form action="verwijderen.php" method="post">
            <?php
                $sql = "SELECT device_id, naam, plaats.plaats, type.type, status FROM devices, type, plaats, status 
                        WHERE devices.type_id = type.type_id 
                        AND plaats.plaats_id = devices.plaats_id
                        AND status.status_id = devices.status_id";
                    $result = $con->query($sql);
                    if ($result->num_rows > 0) 
                    {
                        echo '<table><tr><td>ID</td><td>Naam</td><td>Plaats</td><td>Type</td><td>Status</td><td>Verwijderen</td></tr>';
                        while($row = $result->fetch_assoc()) 
                        {
                            echo '<tr>';
                            echo '<td>' .$row['device_id']. '</td>';
                            echo '<td>' .$row['naam']. '</td>';
                            echo '<td>' .$row['plaats']. '</td>';
                            echo '<td>' .$row['type']. '</td>';
                            echo '<td>' .$row['status']. '</td>';
                            echo '<td>';
            ?>
            <input type="submit" value="Verwijderen" name="submit">
        </form>                             
                <?php '</td>';
                        }
                    echo '</table>';                        
                    }
                ?>
</section>

我拥有的另一个页面是删除插入表中的记录的页面。删除的问题是我找不到删除记录的方法。用$_POST/$_GET在html中将记录添加到数据库中效果良好。。

<?php
error_reporting(E_ERROR);
include '../dbconnect.php';
//$connection = mysql_connect("", "", ""); // Establishing Connection with Server
//$db = mysql_select_db("cerar", $connection); // Selecting Database from Server
    if(isset($_POST['submit']))
    { // Fetching variables of the form which travels in URL
    }       
    mysqli_close($con); // Closing Connection with Server
    //header( "refresh:3;url=index.php" );
?>

为表的每一行创建一个表单,并包含一个隐藏的输入字段,其中包含要传递给服务器的键值。因此,将打开的form标记移到您拥有的input type="submit"之前,并包含另一个隐藏的输入,如下所示:

    <form action="verwijderen.php" method="post">
        <input type="hidden" value="<?=$row['device_id']?>" name="device_id">
        <input type="submit" value="Verwijderen" name="submit">
    </form>                             

然后在PHP中,您可以在$_POST['device_id']中找到必须删除的设备id。写delete语句应该很容易。

<section>
    <form action="verwijderen.php" method="post">
    <?php
        $sql = "SELECT device_id, naam, plaats.plaats, type.type, status FROM devices, type, plaats, status WHERE devices.type_id = type.type_id AND plaats.plaats_id = devices.plaats_id AND status.status_id = devices.status_id";
        $result = $con->query($sql);
        if ($result->num_rows > 0) {
            echo '<table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Naam</th>
                            <th>Plaats</th>
                            <th>Type</th>
                            <th>Status</th>
                            <th>Verwijderen</th>
                        </tr>
                    </thead>
                    <tbody>';
            while($row = $result->fetch_assoc()){
                    echo '<tr>';
                        echo '<td>' .$row['device_id']. '</td>';
                        echo '<td>' .$row['naam']. '</td>';
                        echo '<td>' .$row['plaats']. '</td>';
                        echo '<td>' .$row['type']. '</td>';
                        echo '<td>' .$row['status']. '</td>';
                        echo '<td>';?>
                            <input type="hidden" value="<?php echo $row['device_id']; ?>" name="device_id">
                            <input type="submit" value="Verwijderen" name="submit">
                        <?php '</td>';
            }
            echo '</tbody>
            </table>';
        }
    ?>
    </form>
</section>

PHP代码

<?php
error_reporting(E_ERROR);
include '../dbconnect.php';
extract($_POST);
//$connection = mysql_connect("", "", ""); // Establishing Connection with Server
//$db = mysql_select_db("cerar", $connection); // Selecting Database from Server
    if(isset($_POST['submit']) && $_POST['submit'] == 'Verwijderen')
    {
        //delete query
        //DELETE FROM devices WHERE device_id = $device_id
    }       
    mysqli_close($con); // Closing Connection with Server
    //header( "refresh:3;url=index.php" );
?>