如何使用提交按钮(php)从数​​组中删除条目

时间:2016-08-02 13:02:35

标签: php arrays

以下代码显示变量$ a thru foreach循环的条目,其末尾带有删除按钮,如下所示。 Please click here to see the image.

但是当我点击第一个条目的删除按钮时,系统删除了最后一个条目。请帮我解决这个问题。谢谢。

    $a = $new_batch_marketing->query("SELECT * FROM temporary_container");  

            if(empty($a)){
                #do nothing
            }
            else{
                echo '<table style="width: 400px; height: 400px; resize:none;color:black;background:white;"readonly = "readonly">';
                foreach($a as $key){
                    $z = $key['product_code'];
                    echo '<tr><td>'.$key['product_name'].'&nbsp;&nbsp;'.$key['product_type'].'</td>';
                    echo'<td>'.$key['selling_price'].'</td>';
                    echo'<td>'.$key['case'].'</td>';
                    echo'<td>'.$key['total_selling_price'].'</td><td><input type = "submit" name = "remove" id = "'.$z.'" class =  "form-control" value ="remove" style = "width:60%;"></td></tr>';
                }
                echo '</table>';

                echo'</br></br><input style = "width: 99%;border-radius:2px;" type = "submit" id = "confirm" name = "confirm" value = "confirm" class = "form-control"></br>
                    <input style = "width: 99%;border-radius:2px;" type = "submit" id = "cancel" name = "cancel" value = "cancel" class = "form-control">';

                if(isset($_POST['remove'])){
                        $remove = $new_batch_marketing->query("DELETE FROM temporary_container WHERE `product_code` = '$z'");
                    }
            }

1 个答案:

答案 0 :(得分:4)

试试此代码

$a = $new_batch_marketing->query("SELECT * FROM temporary_container");  

            if(empty($a)){
                #do nothing
            }
            else{
                echo '<table style="width: 400px; height: 400px; resize:none;color:black;background:white;"readonly = "readonly">';
                foreach($a as $key){
                    $z = $key['product_code'];
                    echo '<tr><td>'.$key['product_name'].'&nbsp;&nbsp;'.$key['product_type'].'</td>';
                    echo'<td>'.$key['selling_price'].'</td>';
                    echo'<td>'.$key['case'].'</td>';
                    echo'<td>'.$key['total_selling_price'].'</td>
                    <td><a href="?remove='.$z.'"<input type = "button" name = "remove" id = "" class =  "form-control" value ="remove" style = "width:60%;"></a></td>
                    </tr>';
                }
                echo '</table>';

                echo'</br></br><input style = "width: 99%;border-radius:2px;" type = "submit" id = "confirm" name = "confirm" value = "confirm" class = "form-control"></br>
                    <input style = "width: 99%;border-radius:2px;" type = "submit" id = "cancel" name = "cancel" value = "cancel" class = "form-control">';

                if(isset($_GET['remove'])){
                        $remove = $new_batch_marketing->query("DELETE FROM temporary_container WHERE `product_code` = '$z'");
                    }
            }