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

发布于 2024-12-02 19:19:25 字数 626 浏览 1 评论 0原文

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

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

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

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

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.

$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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抽个烟儿 2024-12-09 19:19:25

您可以使用表单来完成:

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

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

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

You can do it with forms:

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

Or you can do it with jQuery and 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';
      }
    }

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.

淤浪 2024-12-09 19:19:25

我想我可以通过将删除帖子包装在类和函数中来对此进行一点改进。我也遇到了同样的问题。这对我来说非常有用。再次感谢@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: ' . $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>';
        }
    }
?>

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

<?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: ' . $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>';
        }
    }
?>
吝吻 2024-12-09 19:19:25

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文