简体   繁体   中英

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

$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.

So whenever it runs it will only run the last iteration.


Try creating the confirmation function like so (by passing in the 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. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.

You need to write the confirmation js function only once at the end of the page. Much better could be to write it on a separate js file but that is another problem. Make the confirmation function to take one parameter with the id of the record you want to delete.

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;
     } 
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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