Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “删除”按钮无法按id继续刷新页面_Php_Html_Sql - Fatal编程技术网

Php “删除”按钮无法按id继续刷新页面

Php “删除”按钮无法按id继续刷新页面,php,html,sql,Php,Html,Sql,我尝试创建一个删除按钮,但如果我使用复选框,它可以正常工作,但如果没有任何人可以帮助我,则无法工作。我的代码如下: 我添加了表的全部代码,我想通过按钮删除一个条目,希望这更有用 <?php if (isset($_POST['rBtn'])) { $sql = $odb->prepare("DELETE FROM `fe` WHERE `ID` = :id"); $sql->execute(array(':id' => $id)); $noti

我尝试创建一个删除按钮,但如果我使用复选框,它可以正常工作,但如果没有任何人可以帮助我,则无法工作。我的代码如下:

我添加了表的全部代码,我想通过按钮删除一个条目,希望这更有用

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

知识产权
类型
名称
日期
行动

您可以使用每行表单中的按钮,然后使用javascript提交:

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

删除


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

您需要先连接到数据库,然后才能执行任何操作。我看不到您连接到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();
    }
?>

如注释中多次所述,您在提交表单时未向后端发送
$id
的值。有两种可能的解决方法:

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

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

定义“不起作用”。具体失败的是什么?我想从表中删除一个带有按钮的项目。按钮需要获取表条目的每个id…但是会发生什么?请解释您想要做的更全面的事情。什么不适用于给定的代码?你试过调试这个问题吗?请将所有信息添加到问题本身,不到评论部分已经有了它,我在header.php中包含了db.php,它调用所有页面。请不要生气,但我看不到任何包含“db.php”的内容,只针对您princessOk。但它不在给定的代码中,因为它不会导致错误。我需要修复的问题是rBtn代码没有使用按钮Click删除条目。请不要使用
onclick
处理程序。还有更好的方法。此外,请解释为什么您认为它可以解决问题-不需要使用按钮和JS代码提交表单如果我是正确的,那么会删除所有不正确的项目?尼科哈斯:谁说他可以或不可以使用onclick()提交表单。更好的由开发者决定。顺便问一下,你的解决方案在哪里?Private:你可以在删除代码的时候把表单标签包起来,在按钮名或隐藏字段中使用一个n ID,这样你就知道点击了哪个按钮。我只是尝试了一个隐藏字段,但似乎不想wrok@MartinNewman我不知道最初的问题是什么,所以我无法提供解决方案。关于
oncllick
属性与纯JS解决方案的问题,见
<input type="submit" value="Delete" name="rBtn" class="btn btn-outline-danger btn-sm" value="<?php echo (int)$show['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">';
}