简体   繁体   English

PHP删除表行

[英]php deleting a table row

I am writing a booking system for uni and have encountered an issue i need help with. 我正在为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>

This code is for the selecting of which row in the table that you want to delete 此代码用于选择要删除表中的哪一行

<?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>';

}


?>

And this is the code to delete the row 这是删除行的代码

Any suggestions on where i am going wrong as the row won't delete 关于行出错的任何建议都不会删除

it seems like you are passing the tid as a GET variable in the query string but the method of the form is POST. 似乎您正在将tid作为GET变量传递到查询字符串中,但是表单的方法是POST。 Note that both the confirmBooking and denyBooking forms have the same problem 请注意,confirmBooking和denyBooking表单都存在相同的问题

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

should be 应该

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

You have a combination of problems that would cause your record to not delete in the database: 您遇到了多种问题,这些问题将导致您的记录无法在数据库中删除:

  1. Buttons don't send values in POST. 按钮不会在POST中发送值。 You need to add the id to a hidden input and retrieve the value in POST. 您需要将ID添加到隐藏的输入中,然后在POST中检索值。
  2. You've also mixed GET and POST. 您还混合了GET和POST。 Your form method is POST, but you're attempting to retrieve the variable from GET. 您的表单方法是POST,但是您尝试从GET检索变量。 Since the action here is a delete, it's better to use POST (otherwise you need to account for users being able to delete any record by changing an url parameter). 由于此处的操作是删除操作,因此最好使用POST(否则,您需要考虑到用户可以通过更改url参数来删除任何记录)。

Solution: 解:

Create a hidden field and assign it's value using $row['id'] 创建一个隐藏字段并使用$ row ['id']为其赋值

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

And the id will be in $_POST['rowId'] 该ID将在$_POST['rowId']

Other notes: 其他说明:

  1. you should use isset() to verify 'rowId' exists in $_POST before you access it 您应该使用isset()来验证$ _POST中是否存在'rowId',然后再访问它
  2. if rowId should always be an integer, you should get the integer value of the string varaible using intval() before using it in your SQL query. 如果rowId始终为整数,则应在SQL查询中使用intval()之前获取字符串varaible的整数值。
  3. you should use prepared statements with parameter binding in your query. 您应该在查询中使用带参数绑定的准备好的语句

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

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