Php 删除所选项目

Php 删除所选项目,php,sql,Php,Sql,你好。我想删除用户选择的项目。但事实证明,无论用户单击哪个按钮,第一个数据行都在删除。我被困在这里了。请帮忙 这是我的代码: HTML <form method="POST" action="<?php $_PHP_SELF ?>"> <?php $query = "select * from tblsr"; $request = mysql_query($query)or die(mysql_error());?>

你好。我想删除用户选择的项目。但事实证明,无论用户单击哪个按钮,第一个数据行都在删除。我被困在这里了。请帮忙

这是我的代码:

HTML

    <form  method="POST" action="<?php $_PHP_SELF ?>">
<?php

        $query = "select * from tblsr";
        $request = mysql_query($query)or die(mysql_error());?>
        <table class="table table-hover">
        <tr>    
                <th>Softwares</th>
                <th>Difficulty</th>
                <th></th>
                <th></th>
                </tr>
        <?php while($row = mysql_fetch_array($request)){
        ?>
        <tbody>
            <tr class="success">
                <td><?php echo $row['Software']; ?></td>
                <td><?php echo $row['Difficulty']; ?></td>
                <td><input type="submit" class="btn btn-sm btn-success" name="change" value="Change Difficulty"/></td>  
                <td><input type="submit" class="btn btn-sm btn-danger" name="remove" value="Delete"/></td>
                <input type="hidden" name="id[]" value="<?php echo $row['id']; ?>"/>
            </tr>
        </tbody>
        <?php
        }

?>  </form>


您的代码的问题是,您从不增加
$i
$i++
),并且删除查询始终影响第一行(id)。您可以将
$i++
放入
while
循环来修复它,但我建议您只需像下面这样做,不要为循环而烦恼

删除查询:

$rm = $_POST['id'];
if (isset($_POST['remove'])) {
    foreach ($rm as $key => $value) {
        $rm[$key] = "'".$value."'";
    }
    $sql = "Delete FROM tblsr 
                Where id IN(" . implode(",", $rm) .")";
    die($sql);
    $success = mysql_query($sql) or die(mysql_error());

    if ($success) {
        ?>
        <script>
            alert('Deleted.');
            window.location.href = 'manageRequest.php';
        </script>
        <?php

    }

}
$rm=$\u POST['id'];
如果(isset($_POST['remove'])){
foreach($rm as$key=>$value){
$rm[$key]=“'.$value.”;
}
$sql=“从tblsr中删除
其中id位于(“.infrade(“,”,$rm)。”);
死亡($sql);
$success=mysql\u query($sql)或die(mysql\u error());
如果($成功){
?>
警报(“已删除”);
window.location.href='manageRequest.php';
软件
困难

您的代码不完整。我看不出您真正提交表单的位置。首先,请勿放置“首先,请勿放置我获取所选行id的部分在哪里?它位于
$\u POST['remove']
。请尝试回显查询,以确定我是否尝试了您的代码,并确认“已删除”“但是没有执行查询,我回显了$\u POST['remove'],它没有显示任何内容,因此为空。请尝试更新的答案,然后尝试
printr($\u POST['remove'])
not
echo
$rm = $_POST['id'];
if (isset($_POST['remove'])) {
    foreach ($rm as $key => $value) {
        $rm[$key] = "'".$value."'";
    }
    $sql = "Delete FROM tblsr 
                Where id IN(" . implode(",", $rm) .")";
    die($sql);
    $success = mysql_query($sql) or die(mysql_error());

    if ($success) {
        ?>
        <script>
            alert('Deleted.');
            window.location.href = 'manageRequest.php';
        </script>
        <?php

    }

}
<form  method="POST" action="<?php $_PHP_SELF ?>">
    <?php
    $query = "select * from tblsr";
    $request = mysql_query($query) or die(mysql_error());
    ?>
    <table class="table table-hover">
        <tr>    
            <th>Softwares</th>
            <th>Difficulty</th>
            <th></th>
            <th></th>
        </tr>
        <?php
        while ($row = mysql_fetch_array($request)) {
            ?>
            <tbody>
                <tr class="success">
                    <td><?php echo $row['Software']; ?></td>
                    <td><?php echo $row['Difficulty']; ?></td>
                    <td>
                    <input type="checkbox" name="id[]" value="<?php echo $row['id']; ?>"/>
                    delete
                    </td>
            </tr>
            </tbody>
            <?php
        }
        ?> 
    </table>
    <button type="submit" name="remove" class="btn  btn-success">
        delete checked user
    </button>
</form>