【问题标题】:php delete mySQL row from <table>php 从 <table> 中删除 mySQL 行
【发布时间】:2013-07-12 15:45:58
【问题描述】:

我正在创建一个表,每一行都有一个删除按钮,它应该删除该行。但是,现在,当我单击删除时,“什么都没有发生”,我必须刷新页面才能看到结果。请帮忙。

我的代码 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>

删除.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

<?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();
}

http://www.w3dominik.com/x/phptest/ 现场演示

【问题讨论】:

  • 你是否连接到delete.php文件中的数据库?
  • 如果您在两个选项卡/窗口中打开页面并在第一个选项卡中删除一行并刷新该选项卡,那么当您在第二个选项卡中按下删除(在该行)时,它会起作用。换句话说,我们需要看到更多的代码......你能复制所有的index.phpdelete.php吗? (可能connect.php 删除您的详细信息等)
  • 您应该为此使用 AJAX [请参阅] {openenergymonitor.org/emon/node/107}
  • Ajax 可以用于此,没有理由应该使用它。提交表单并重定向回页面是一种完全可以接受的做事方式......

标签: php


【解决方案1】:

查看connect.php 并确保没有输出。 header() 如果前面有任何空格或空行输出,则将不起作用。很容易错误地输入某种空白字符。 http://php.net/manual/en/function.header.php

【讨论】:

  • 好的,现在,我已经添加了整个 index.php 和 connect.php。现在呢?
  • 正如文档所说:'请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通 HTML 标记、文件中的空白行还是从 PHP 发送。使用 include 或 require 函数或其他文件访问函数读取代码是一个非常常见的错误,并且在调用 header() 之前会输出空格或空行。文件中有许多空行,但很难判断它们是否导致了问题。我会尽可能内联(没有include),看看你是否可以解决问题
  • 这是错误的。表单提交到另一个页面,然后被重定向回。如果header 重定向不起作用,那么它会抛出错误并指出headers have already been sent 并且不会重定向回表单页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多