2013-07-01 24 views
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> 

有什麼建議嗎?

+0

是否有問題? – str

+0

你不需要這種形式,只是創建類似鏈接:''Delete PaulRoth

+0

@PaulRoth你需要使用'urlencode'正確編碼GET參數。 –

回答

0

而不是具有值作爲td的一部分添加的隱藏輸入元件:

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

既然這樣delete.php沒有接收id的值。

+0

速記echo語句也是一個選項'<?= $ article ['article_id'];?>'。這是由新PHP版本中的標準啓用的。 –

+0

@Jim我嘗試了你所說的,但沒有奏效。我不知道問題是什麼? – Timberman

+0

@Timberman是否delete.php獲取ID? – Jim

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