显示mysql表中的所有行,然后提供删除特定行的选项

时间:2011-09-04 22:34:29

标签: php mysql delete-row corresponding-records

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

我目前正在使用for循环显示每个条目的数据库的每个循环。

$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";
 }

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

3 个答案:

答案 0 :(得分: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>

<强> // delete.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>

<强> // delete.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';
      }
    }

这些都是未经测试的,确定需要对您的具体项目进行一些调整,但我认为您明白这一点,我希望它有所帮助。

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

答案 1 :(得分: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>';
        }
    }
?>

答案 2 :(得分:0)

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