dongzituo5530 2014-09-10 13:09
浏览 25
已采纳

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

I have a database setup to register members for a members only area of a site. I can echo all of the registered members with a checkbox so that I can choose to delete an individual member from an admin page, but I cant seem to figure out how to get the member chosen to be deleted when the submit button is clicked. I have tried to do it on a single page and on a 2 page process, first page lists the members with the checkbox which works to the point of choosing the member to be deleted, the difficulty I seem to be having is getting the members detail passed to the delete section of the code. Could anyone assist me please.

here is the delete_user.php which lists the members with a checkbox

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

here is the process page 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条回答 默认 最新

  • douya1974 2014-09-10 13:21
    关注

    You must render your checkboxes inside your form:

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

    Then on your delete process:

    <?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 />';           
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像