简体   繁体   English

PHP的从删除MySQL行<table>

[英]php delete mySQL row from <table>

I'm creating a table, each row has a button delete, which is supposed to delete the row. 我正在创建一个表,每行都有一个删除按钮,应该删除该行。 However, now, when I click delete, "nothing happens" and I have to refresh the page to see the results. 但是,现在,当我单击“删除”时,“什么也没有发生”,我必须刷新页面才能看到结果。 Please Help. 请帮忙。

My code index.php 我的代码index.php

<body>

    <?php include "include/connect.php"; ?>

    <table>
        <tr>
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Proffesion</th>
            <th>Rank</th>
            <th>Options</th>
            <th>&nbsp;</th>
        </tr>
        <tr>
            <form action="add.php" method="post">
                <td><input type="text" name="id" disabled size="5"></td>
                <td><input type="text" name="fname"></td>
                <td><input type="text" name="lname"></td>
                <td><input type="text" name="prof"></td>
                <td><input type="text" name="rank" size="5"></td>
                <td><input type="submit" value="add" name="watta"></td>
                <td>&nbsp;</td>
            </form>
        </tr>

        <?php
            $query = "SELECT * FROM staff";
            $vysledek = mysqli_query($link, $query);

            while ($udaj = mysqli_fetch_array($vysledek)):

                if($udaj[4]==0){
                    echo "<tr class='zero'>";
                } else {
                    echo "<tr>";    
                }



                echo "<td>" . $udaj[0] . "</td>";
                echo "<td>" . $udaj[1] . "</td>";
                echo "<td>" . $udaj[2] . "</td>";
                echo "<td>" . $udaj[3] . "</td>";
                echo "<td>" . $udaj[4] . "</td>";
                echo "<td class='btn'><a href=''>";

                ?>
                <form action="delete.php" method="get">
                    <input name="id" type="hidden" value="<?php echo $udaj[0]; ?>">
                    <input name="what" type="submit" value="DELETE">
                </form>

                <?php
                echo "</a></td>";
                echo "<td class='btn'><a href=''>edit</a></td>";

                echo "</tr>";

            endwhile;


        ?>

    </table>

</body>

delete.php delete.php

<?php

include "include/connect.php";

$toId = ($_GET["id"]);

$queryy = "DELETE FROM staff WHERE id=$toId";
$vysledek = mysqli_query($link, $queryy);

header('Location: http://www.w3dominik.com/x/phptest/');

connect.php connect.php

<?php

include "config.php";

$link = mysqli_connect(SQL_HOST, SQL_USERNAME, SQL_PASSWORD, SQL_DBNAME);
mysqli_set_charset($link, "utf8");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

Live demo at http://www.w3dominik.com/x/phptest/ 现场演示, 网址http://www.w3dominik.com/x/phptest/

Look at connect.php and make sure there is NO output. 查看connect.php并确保没有输出。 header() won't work if there is any blank spaces or empty lines output before it. 如果在其之前输出任何空格或空行,则header()将不起作用。 Very easy to have some kind of blank characters put in by mistake. 误输入某些空白字符非常容易。 http://php.net/manual/en/function.header.php http://php.net/manual/zh/function.header.php

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

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