Php 如何将表单更改为删除帖子

Php 如何将表单更改为删除帖子,php,Php,我有一个用下拉列表删除帖子的表单。它很好用 delete.php 选择要删除的项目 我想改变这一点,以便我的帖子在一个带有删除按钮的表格中一个比另一个高 这是我的尝试: <h4>Select an post to delete</h4> <form action="delete.php" method="get"> <?php foreach ($articles as $article) {?>

我有一个用下拉列表删除帖子的表单。它很好用

delete.php



选择要删除的项目
我想改变这一点,以便我的帖子在一个带有删除按钮的表格中一个比另一个高 这是我的尝试:

<h4>Select an post to delete</h4>
    <form action="delete.php" method="get">
           <?php foreach ($articles as $article) {?>
          <tr>
        <td value="<?php echo $article['article_id'];?>"><?php echo $article['article_title'];?></td>
        <td><a href="#" onclick="this.form.submit();" name="id">Delete</a></td></br>
          </tr>
           <?php } ?>
    </form>
选择要删除的帖子


有什么建议吗?

不要将值作为td的一部分,而是添加一个隐藏的输入元素:

<input type="hidden" name="id" value="<?php echo $article['article_id'];?>">

以下是一个完整的示例:

<?php
session_start();
include_once('../includes/conection.php');
include_once('../includes/article.php');

$article = new Article;

if (isset($_SESSION['logged_in'])) {
    if (isset($_GET['id'])) {
        $id = $_GET['id'];
        $query = $pdo->prepare('DELETE FROM articles WHERE article_id = ?');
        $query->bindValue(1, $id);
        $query->execute();
        header('Location: delete.php');
    }
    $articles = $article->fetch_all();
?>

<html>
<head>
    <title></title>
    <link href="../assets/style.css" rel="stylesheet"/>
</head>
<body>
<div class="container">
    <a href="index.php" id="logo"></a>
    <br/>
    <h4>Select an article to delete</h4>
    <? foreach ($articles as $article): ?>
        <a href="delete.php?id=<? print urlencode($article['article_id']) ?>">
            <?php print $article['article_title'] ?>
        </a>
    <? endforeach; ?>
</div>
</body>
</html>

<?php
} else {
    header('Location: index.php');
}
?>


选择要删除的项目
您不需要此表单,只需创建如下链接:
@PaulRoth您需要使用
urlencode
@PaulRoth对GET参数进行正确编码。我尝试了您所说的,但没有用,现在我将整个文件放在这里delete.php,如果您有时间的话look@MarcelKorpel我假设ID总是一个数字。在这种情况下(我认为)不需要编码。速记回显语句也是一个选项
。在新的PHP版本中,这是标准启用的。@Jim我尝试了你所说的,但没有成功。我不知道是什么问题?
<?php
session_start();
include_once('../includes/conection.php');
include_once('../includes/article.php');

$article = new Article;

if (isset($_SESSION['logged_in'])) {
    if (isset($_GET['id'])) {
        $id = $_GET['id'];
        $query = $pdo->prepare('DELETE FROM articles WHERE article_id = ?');
        $query->bindValue(1, $id);
        $query->execute();
        header('Location: delete.php');
    }
    $articles = $article->fetch_all();
?>

<html>
<head>
    <title></title>
    <link href="../assets/style.css" rel="stylesheet"/>
</head>
<body>
<div class="container">
    <a href="index.php" id="logo"></a>
    <br/>
    <h4>Select an article to delete</h4>
    <? foreach ($articles as $article): ?>
        <a href="delete.php?id=<? print urlencode($article['article_id']) ?>">
            <?php print $article['article_title'] ?>
        </a>
    <? endforeach; ?>
</div>
</body>
</html>

<?php
} else {
    header('Location: index.php');
}
?>