簡體   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