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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7302554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:25:51  来源:igfitidea点击:

Show all rows in mysql table then give option to delete specific ones

phpmysqldelete-rowcorresponding-records

提问by DIM3NSION

I want to have the ability to show all the entries in a database table and by each one give the user the ability to delete specific ones.

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

I am currently using a for each loop that loops through the database showcasing each entry.

我目前正在使用 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";
 }

How would to add a delete button that appears by each one and removes the entry from the database table?

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

回答by Quasdunk

You can do it with forms:

你可以用表格来做:

//main.php

//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:

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


Or you can do it with jQuery and AJAX:

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

//main.php

//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

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


It's all untested and sure needs some adjustment for your specific project, but I think you get the idea and I hope it helps.

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

Next time, please post your schema if you ask stuff about database.

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

回答by Kyle Coots

I thought I would improve on this a little bit by wrapping the delete post in a class and function. I was having the same problem. and this worked great for me. Thanks again @ Quasdunk

我想我会通过将删除帖子包装在一个类和函数中来稍微改进一下。我遇到了同样的问题。这对我很有用。再次感谢@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>';
        }
    }
?>

回答by Uday Hiwarale

Produce a key to each table, using jquery,then link it to a php file which an accept the key and delete from the specific table (which also can be passed through jquery)

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