2013-08-28 84 views
6

我有一個來自我的數據庫的項目表和我想要刪除按鈕的每個項目。每個字段的刪除按鈕

目前它正在工作,但按鈕顯示ID號,我想它只是說「刪除」。

我還是希望它的基礎上刪除了joke.ID

$result = mysqli_query($con, "SELECT joke.id, joketext, jokedate, name, email FROM joke INNER JOIN author ON authorid = author.id"); 

echo "<form action='delete1.php' method='post'> 
<table border='1'> 
    <tr> 
     <th>Joke</th> 
     <th>Date</th> 
     <th>Name</th> 
     <th>Email</th> 
     <th>Delete</th> 
    </tr>"; 

    while($row = mysqli_fetch_array($result)) { 
     echo "<tr>"; 
     echo "<td>" . $row['joketext'] . "</td>"; 
     echo "<td>" . $row['jokedate'] . "</td>"; 
     echo "<td>" . $row['name'] . "</td>"; 
     echo "<td>" . $row['email'] . "</td>"; 
     echo "<td><input type='submit' name='deleteItem' value='".$row['id']."' /></td>"; 
     echo "</tr>"; 
    } 
    echo "</table>"; 
    echo "</form></br>"; 

    mysqli_close($con); 
?> 

這是delete1.php

if (isset($_POST['deleteItem']) and is_numeric($_POST['deleteItem'])) { 
     // here comes your delete query: use $_POST['deleteItem'] as your id 
     mysqli_query($con,"DELETE FROM joke WHERE id='$_POST[deleteItem]'"); 
    } 
    mysqli_close($con); 
    header('Location: joke1.php'); 
    exit(); 
?> 

我只想按鈕,說 「刪除」

回答

7

圍棋另一邊。做一個隱藏的輸入元素如下:

echo "<input type='hidden' name='deleteItem' value='".$row['id']."'>"; 

並改變現有的一個

echo "<td><input type='submit' value='Delete' /></td>"; 

就是這樣。編輯1:你將不得不爲每個笑話保留一個不同的表格。

while($row = mysqli_fetch_array($result)) 
{ 

    echo "<tr>"; 
    echo "<td>" . $row['joketext'] . "</td>"; 
    echo "<td>" . $row['jokedate'] . "</td>"; 
    echo "<td>" . $row['name'] . "</td>"; 
    echo "<td>" . $row['email'] . "</td>"; 
    echo "<td>"; 
    echo "<form action='delete1.php' method='post'> 
    echo "<input type='hidden' name='deleteItem' value='".$row['id']."'>"; 
    echo "<input type='submit' value='Delete' />"; 
    echo "</form>"; 
    echo "</td>"; 
    echo "</tr>"; 
} 
+0

的HTML在你的* EDIT1 *是真正可怕的。只需將表單,隱藏輸入和提交按鈕全部放在最後一個表格單元中 – Phil

+0

做到了。謝謝。 – kushpf

+0

你剛剛錯過了EDIT1中的一點語法,除了那個工程很棒,謝謝 –

0

用簡單的方法

echo "<td><input type='button' value='Delete' onclick='javascript:location.href=\'delete1.php?id=" .$row['id']. "\'' /></td>"; 

delete1.php

if(isset($_GET['id']) and is_numeric($_GET['id']))