删除在成员数据库中注册的成员

时间:2014-09-10 13:09:43

标签: php html mysql

我有一个数据库设置来注册站点的成员区域的成员。我可以通过一个复选框回显所有已注册的成员,以便我可以选择从管理页面中删除单个成员,但我似乎无法弄清楚如何在单击提交按钮时选择删除该成员。我试图在一个页面和一个2页的过程中完成它,第一页列出了具有复选框的成员,这对于选择要删除的成员起作用,我似乎遇到的困难是获得成员的详细信息传递给代码的删除部分。任何人都可以帮助我。

这里是delete_user.php,它列出了带有复选框的成员

   <?php 
    include_once 'db_connect.php';
    include_once 'functions.php';
    sec_session_start();

    //display users info with checkbox to delete  
    $sql = "SELECT * FROM `members` LIMIT 0, 30 ";  
    $result = mysqli_query($mysqli, $sql);  
    while($row = mysqli_fetch_array($result))
    { 
        echo '<input type="checkbox" value="' .$row['username'] . '" name="delete[]" />';     
//      echo '<input type='checkbox' value='' .$row['adminid'] . '' name='delete[]' />';
        echo ' ' .$row['username'];
        echo ' ' .$row['email'];
        echo '<br />';  
    }
    ?>
    <!doctype html>
    <html>
        <head>
          <meta charset="utf-8">
          <title>Delete Member</title>
        </head>
        <body>
          <form>
            <input name="submit" type="submit" formaction="delete_user.inc.php" formmethod="POST" value="Delete User">
          </form>
        </body>
    </html>

这是进程页面delete_user.inc.php

   <?php
   include_once 'db_connect.php';
   include_once 'functions.php';
   sec_session_start();
   if ($_SERVER["REQUEST_METHOD"] == "POST")    {          
        foreach($_POST['delete'] as $delete_user)
        {   
            $sql = "DELETE FROM members WHERE memberid = '$delete_user'";      
            mysqli_query($mysqli, $sql) or die ('die now');          
        }        
        echo 'user has been deleted.<br />';   
    }
    ?>

1 个答案:

答案 0 :(得分:1)

您必须在表单中呈现您的复选框:

<?php 
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start();

//display users info with checkbox to delete  
$sql = "SELECT * FROM `members` LIMIT 0, 30 ";  
$result = mysqli_query($mysqli, $sql);

?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Delete Member</title>
    </head>
    <body>
        <form type="POST" action="delete_user.inc.php">
        <?php while($row = mysqli_fetch_array($result)): ?>
            <label>
            <?php echo $row['username']; ?> - <?php echo $row['email']; ?>
            <input type="checkbox" value="<?php echo $row['memberid']; ?>" name="delete[]" />
            </label>
        <?php endwhile; ?>
        <input name="submit" type="submit" value="Delete User" />
        </form>
    </body>
</html>

然后在删除过程中:

<?php
include_once 'db_connect.php';
include_once 'functions.php';

sec_session_start();

if (isset($_POST['submit'])) {          
    foreach($_POST['delete'] as $delete_user) {   
        $sql = "DELETE FROM members WHERE memberid = ?";      
        $stmt = $mysqli->prepare($sql);
        $stmt->bind_param('i', $delete_user);
        $stmt->execute();
    }
    echo 'user has been deleted.<br />';           
}

?>