5

我希望能够显示数据库表中的所有条目,并且每一个都让用户能够删除特定的条目。

我目前正在使用一个 for each 循环,该循环遍历显示每个条目的数据库。

$result = mysql_query("SELECT * FROM KeepScores");


$fields_num = mysql_num_fields($result);

echo "<table><tr>";
// printing table headers


echo "<td>Recent Posts</td>";

echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";

// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
    echo "<td>$cell</td>";

echo "</tr>\n";
 }

如何添加每个出现的删除按钮并从数据库表中删除条目?

4

3 回答 3

11

你可以用表格来做到这一点:

//main.php

<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>

<table>
  <tr>
    <td>Recent Posts</td>
  </tr>
  <?php while($row = mysql_fetch_array($result)) : ?>
  <tr>
    <td><?php echo $row['field1']; ?></td>
    <td><?php echo $row['field2']; ?></td>
    <!-- and so on -->
    <td>
      <form action="delete.php" method="post">
        <input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
        <input type="submit" value="Delete" />
      </form>
    </td>
  </tr>
  <?php endwhile; ?>
</table>

//删除.php:

<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
  $delete_id = mysql_real_escape_string($_POST['delete_id']);
  mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
  header('Location: main.php');
}

或者你可以使用 jQuery 和 AJAX 来实现:

//main.php

<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>

<table>
  <tr>
    <td>Recent Posts</td>
  </tr>
  <?php while($row = mysql_fetch_array($result)) : ?>
  <tr id="<?php echo $row['id']; ?>">
    <td><?php echo $row['field1']; ?></td>
    <td><?php echo $row['field2']; ?></td>
    <!-- and so on -->
    <td>
      <button class="del_btn" rel="<?php echo $row['id']; ?>">Delete</button>
    </td>
  </tr>
  <?php endwhile; ?>
</table>

<script>
  $(document).ready(function(){
    $('.del_btn').click(function(){
       var del_id = $(this).attr('rel');
       $.post('delete.php', {delete_id:del_id}, function(data) {
          if(data == 'true') {
            $('#'+del_id).remove();
          } else {
            alert('Could not delete!');
          }
       });
    });
  });
</script>

//删除.php

<?php
    if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
      $delete_id = mysql_real_escape_string($_POST['delete_id']);
      $result = mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
      if($result !== false) {
        echo 'true';
      }
    }

这一切都未经测试,并且确实需要针对您的特定项目进行一些调整,但我认为您明白了,希望对您有所帮助。

下次,如果您询问有关数据库的内容,请发布您的架构。

于 2011-09-04T23:32:35.297 回答
1

我想我会通过将删除帖子包装在一个类和函数中来改进这一点。我遇到了同样的问题。这对我很有用。再次感谢@Quasdunk

<?php

    // Class to hold the remove post function 
    class someClass{

        //Function for removing the post
        function removePost(){
            if(isset($_POST['delete_id']) && (!empty($_POST['delete_id']))) {
                $delete_id = mysql_real_escape_string($_POST['delete_id']);

                $result = mysql_query("DELETE FROM post WHERE post_id='".$delete_id."' AND post_member='" . $_SESSION['SESS_USER'] . "'");
                if($result !== false) {
                    echo 'true';
                }
            }
        }
    }

    if(isset($_SESSION['SESS_MEMBER_ID'])){
        $member = $_SESSION['SESS_USER'];
        $res = mysql_query("SELECT * FROM post WHERE post_member='$member' ORDER BY timestamp DESC") or die(mysql_error());
        $i = new someClass;
        while($row = mysql_fetch_array($res)){

            echo '<div style="width:100%;margin:0 auto;border-top:thin solid #000;">';
            echo '<div style="width:600px;margin:0 auto;padding:20px;">';

            echo $row['post_text'] . '<br>';
            $postID = $row['post_id'];
            echo '<div style="border-top:thin solid #000;padding:10px;margin-top:5px;background-color:#CCC;">';
            echo 'You posted this on:&nbsp;' . $row['post_date'] . '@' . $row['post_time'];
            echo '<div style="float:right;">
                    <form method="post" action="'. $i->removePost() .'">
                      <input type="hidden" name="delete_id" value="'.$row['post_id'].'" >
                      <input type="submit" value="Delete Post">
                    </form>
                  </div>';
            echo '</div>';

            echo '</div>';
            echo '</div>';
        }
    }
?>
于 2012-08-12T14:53:57.870 回答
0

使用 jquery 为每个表生成一个密钥,然后将其链接到一个接受密钥并从特定表中删除的 php 文件(也可以通过 jquery 传递)

于 2013-10-25T10:17:23.797 回答