繁体   English   中英

PHP删除表行

[英]php deleting a table row

我正在为uni编写预订系统,并且遇到了需要帮助的问题。

<?php
        $title = 'Room Booking';
        require_once 'header.php';
    ?>
    <ul>
    <?php

        $query = "SELECT * FROM `room1booking` ORDER BY date, start, id";

        if ($result = mysqli_query($db_server, $query)) {
            if (mysqli_num_rows($result) > 0) {    

                while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
                    echo '<li>
                            ' . $row['name'] . '
                            ' . $row['date'] . '
                            ' . $row['start'] . '
                        <form action="confirmBooking.php?tid=' . $row['id'] . '" method="post">
                            <button type="submit" name="submit"><a href="confirmBooking.php?tid=' . $row['id'] . '">Confirm</a></button>
                            </form>
                            <form action="denyBooking.php?tid=' . $row['id'] . '" method="post">
                            <button type="submit" name="submit"><a href="denyBooking.php?tid=' . $row['id'] . '">Deny</a></button>
                         </form>
                         </li>
                         ';

                }

            } else {
                echo '<li>There are no results</li>';
            }

            mysqli_free_result($result);
        }


        mysqli_close($db_server);
    ?>
    </ul>
    </form>

此代码用于选择要删除表中的哪一行

<?php
$title = 'Delete The Booking';
    require_once 'header.php';

    if(isset($_GET['submit'])){

        $tid = mysqli_real_escape_string($db_server, trim($_GET['tid']));

        if(!empty($tid)){

            $query = "DELETE FROM room1booking WHERE id = '$tid'";

            mysqli_query($db_server, $query);

            echo '<p>You have successfully deleted the booking.</p>';

    }
    } else {
        echo '<p>There is a problem with the system.</p>';

}


?>

这是删除行的代码

关于行出错的任何建议都不会删除

似乎您正在将tid作为GET变量传递到查询字符串中,但是表单的方法是POST。 请注意,confirmBooking和denyBooking表单都存在相同的问题

<form action="denyBooking.php?tid=' . $row['id'] . '" method="post">

应该

 <form action="denyBooking.php?tid=' . $row['id'] . '" method="get">

您遇到了多种问题,这些问题将导致您的记录无法在数据库中删除:

  1. 按钮不会在POST中发送值。 您需要将ID添加到隐藏的输入中,然后在POST中检索值。
  2. 您还混合了GET和POST。 您的表单方法是POST,但是您尝试从GET检索变量。 由于此处的操作是删除操作,因此最好使用POST(否则,您需要考虑到用户可以通过更改url参数来删除任何记录)。

解:

创建一个隐藏字段并使用$ row ['id']为其赋值

<input type="hidden" name="rowId" value="' . $row['id'] . '">

该ID将在$_POST['rowId']

其他说明:

  1. 您应该使用isset()来验证$ _POST中是否存在'rowId',然后再访问它
  2. 如果rowId始终为整数,则应在SQL查询中使用intval()之前获取字符串varaible的整数值。
  3. 您应该在查询中使用带参数绑定的准备好的语句

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM