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

时间:2013-07-01 15:50:08

标签: php

我有一个表单来删除带有下拉列表的帖子。它工作得很好......

delete.php

<?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>
    <form action="delete.php" method="get">
        <select onchange="this.form.submit();" name="id">
           <?php foreach ($articles as $article) {?>
           <option value="<?php echo $article['article_id'];?>"><?php echo $article['article_title'];?></option>
           <?php } ?>
        </select>   
    </form>
</div>
</body>
</html>

<?php
}else {
header('Location: index.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>

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

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

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

因为它代表delete.php没有收到id值。

答案 1 :(得分:0)

这是一个完整的例子:

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