ID的删除按钮无效,页面保持刷新

时间:2019-03-06 14:26:34

标签: php html sql

我尝试制作一个删除按钮,但是如果我使用复选框,它可以正常工作,但是如果没有人可以帮助我,我的代码如下:

我在表中添加了我的整个代码,希望通过按钮删除条目,希望这样更有用。

 <?php

if (isset($_POST['rBtn'])) {
    $sql = $odb->prepare("DELETE FROM `fe` WHERE `ID` = :id");
    $sql->execute(array(':id' => $id));
    $notify = '<div class="btn btn-outline-success btn-sm" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><i class="mdi mdi-check-all"></i>API has been deleted!</div><meta http-equiv="refresh" content="2;url=customers.php">';
}
?>

<div class="card-body">
    <form action="" method="POST" class="form-horizontal">
        <table id="bootstrap-data-table-export" class="table table-striped table-bordered">
            <thead>
            <tr>
                <th>IP</th>
                <th>Type</th>
                <th>Name</th>
                <th>Date</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
            <?php
            $SQLSelect = $odb->prepare("SELECT * FROM `fe` WHERE `userID` = :user ORDER BY `ID` DESC");
            $SQLSelect->execute(array(':user' => $_SESSION['ID']));
            while ($show = $SQLSelect->fetch(PDO::FETCH_ASSOC)) {
                $ipShow   = htmlspecialchars($show['ip']);
                $noteShow = htmlspecialchars($show['note']);
                $ids      = intval($show['ID']);
                $date     = htmlspecialchars(date("d-m-Y, h:i:s a", $show['date']));
                $type     = $show['type'] == 'f' ? '<button class="btn btn-success btn-sm">Friend</button>' : '<button class="btn btn-danger btn-sm">Enemy</button>';
                echo '<tr><td>' . htmlspecialchars($ipShow) . '</td><td>' . $type . '</td><td>' . htmlspecialchars($noteShow) . '</td><td>' . htmlspecialchars($date) . '</td><td><input type="submit" value="Delete" name="rBtn" class="btn btn-outline-danger btn-sm" /></td></tr>';
            }
            ?>
            </tbody>
        </table>
    </form>
</div>

3 个答案:

答案 0 :(得分:2)

可能是因为没有与数据库的连接。

您需要先连接到数据库,然后才能执行任何操作。我看不到连接到sql数据库的任何位置。

要以数据库PDO方式连接,可以使用:

<?php
$servername = "localhost" //by default is set to localhost;
$username = "username"//or whatever your username is;
$password = "password" //or whatever your password is;

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>

答案 1 :(得分:1)

正如评论中多次提到的那样,您尚未在提交表单时向后端发送$id的值。解决此问题的方法有两种:

1)使用模板中的以下部分通过按钮将其发送:

<input type="submit" value="Delete" name="rBtn" class="btn btn-outline-danger btn-sm" value="<?php echo (int)$show['ID']; ?>"/>

然后,请先读取该ID值:

if (isset($_POST['rBtn'])) {
    $id = $_POST['rBtn'];
    $sql = $odb->prepare("DELETE FROM `fe` WHERE `ID` = :id");
    $sql->execute(array(':id' => $id));
    $notify = '<div class="btn btn-outline-success btn-sm" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><i class="mdi mdi-check-all"></i>API has been deleted!</div><meta http-equiv="refresh" content="2;url=customers.php">';
}

2)每行使用一个表格,并将该ID值放在要传输的隐藏字段中

此外,请看一下调试-它确实可以帮助您下​​次自行发现此类错误。您将简单地看到,鉴于您共享的代码是完整的,变量$id没有任何价值。其次,该查询的执行将引发错误(这将告诉您缺少某些内容)。

答案 2 :(得分:-1)

您可以在表格的每一行中使用一个按钮,然后使用javascript提交:

<form action="...">  
    <input name="id" value="5"/>
    <button name="del" onclick="javascript:this.form.submit();">Delete</button>
</form>

...

<?php
if (isset($_POST['del']) {
    ...
}
?>