简体   繁体   English

为什么删除按钮不断删除最新行的PHP

[英]why delete button keeps deleting latest row php

hi im trying to delete using confirmation script that connect with php , but its always deleting from the latest row that i input , im already search online how to fix it , but seems i still not really understand how it work , here is my code 您好,即时通讯正在尝试使用与php连接的确认脚本进行删除,但它始终会从我输入的最新行中删除,即时通讯已经在网上搜索了如何解决该问题,但是我似乎仍然不太了解其工作原理,这是我的代码

    <?php 
        $query = "SELECT * FROM mspartner";
        $sql=mysql_query($query);
    ?>          
        <table class="show_data" id="partner">
            <tr>        
            <td>Partner ID</td>
                <td>Partner name</td>
                <td>addresses</td>
                <td>Image</td>
                 <td>Delete/Edit</td>
            </tr>
            <?php 
                while($fetch=mysql_fetch_array($sql)){
            ?>
            <tr>

                <td> <?php echo $fetch['partnerID']; ?></td>
                <td style="width:100px;"><?php echo $fetch['partnername']; ?></td>
                <td><?php echo $fetch['address']; ?></td>
                <td><?php echo $fetch['image']; ?></td>

                <td>
                 <script>
                    function confirmation(){
                        var message = "are you sure?";
                        var konfirmasi = confirm(message);
                        if(konfirmasi == true) {
                                 window.location="index.php?delete_partner=<?php echo $fetch['partnerID']; ?>";
                        }
                        else {

                        }
                    }
                </script>
                <input type="image" class="icon" src="images/asset/delete.jpg" onClick="confirmation() ">   

and here is my php delete code 这是我的PHP删除代码

             <?php 

$id = $_GET['delete_partner'];
$sql="DELETE FROM `mspartner` WHERE `partnerID` = '$id' ";
echo $sql;
$query = mysql_query($sql);
header('Location:index.php?partner');
          ?>

thanks for your help 谢谢你的帮助

You keep replacing your confirmation with a new confirmation function. 您一直用新的confirmation功能替换您的confirmation

So whenever it runs it will only run the last iteration. 因此,无论何时运行,它只会运行最后一次迭代。


Try creating the confirmation function like so (by passing in the id): 尝试像这样创建确认功能(通过传入ID):

function confirmation(id){
    var message = "are you sure?";
    var konfirmasi = confirm(message);
    if(konfirmasi) {
        window.location="index.php?delete_partner=" + id;
    }
    else {
        // something else
    }
}

Side point: 侧面点:

There is no more support for mysql_* functions, they are officially deprecated , no longer maintained and will be removed in the future. 不再支持 mysql_*函数,它们已被正式弃用不再维护 ,以后将被删除 You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. 您应该使用PDOMySQLi更新代码,以确保将来项目的功能。

You need to write the confirmation js function only once at the end of the page. 您只需在页面末尾编写一次确认js函数。 Much better could be to write it on a separate js file but that is another problem. 最好将它写在一个单独的js文件上,但这是另一个问题。 Make the confirmation function to take one parameter with the id of the record you want to delete. 使确认功能采用一个参数,该参数具有要删除的记录的ID。

Replace the code that calls the function with this: 用以下代码替换调用该函数的代码:

onClick="confirmation('<?php echo $fetch['partnerID']; ?>')"

And your new confirmation function should be: 您的新确认功能应为:

   function confirmation(recordId){
     var message = "are you sure?";
     var konfirmasi = confirm(message);
     if(konfirmasi == true) {
        window.location="index.php?delete_partner=" +  recordId;
     } 
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM