0

ドロップダウン選択リストを使用して投稿を削除するフォームがあります。それは正常に動作します...

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>

助言がありますか?

4

2 に答える 2

0

td非表示の入力要素の追加の一部として値を使用するのではなく、次のようにします。

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

現状では、delete.php は id 値を受け取っていません。

于 2013-07-01T15:54:01.723 に答える
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');
}
?>
于 2013-07-02T12:05:39.087 に答える