简体   繁体   中英

Delete Button (to delete rows in phpmyadmin table)

I am doing a website with login system were each user can insert and view his logs.

Now I am trying to make a delete button that when clicked an entire row from a table is deleted.

It is working fine because when I click the "DELETE BUTTON" and check the rows from thee phpmyadmin shell the row is no longer there.

The problem is that on the first click the row is still visible in the webpage and only when I click the button again it disappears!

Here is my code: -

<?php   
    $entries = mysql_query("SELECT `id`,`date`, `location`, `description` FROM `logs` WHERE `username` = '$current_user'");


if(mysql_num_rows($entries)==0){

    echo 'No entries, yet.';}
    else{


        while($entries_row = mysql_fetch_assoc($entries)){          
            echo    " 
                <hr> 
                <form action='' method='post'> 
                <input type='hidden' name='id' value='".$entries_row['id']."'> 
                <table align='center' border='1' width='80%' cellpadding='5'> 
                <tr> 
                <td width='20%'><strong>Date:</strong></td> 
                <td>" . $entries_row['date'] . "</td> 
                </tr>     
                <tr> 
                <td width='20%'><strong>Location:</strong></td> 
                <td>" . $entries_row['location'] . "</td> 
                </tr> 
                <tr> 
                <td width='20%'><strong>Description:</strong></td> 
                <td>" . $entries_row['description'] . "</td> 
                </tr> 
                <tr> 
                <td colspan='2' align='right'><input type='submit' name='delete' value='Delete Entry'></td> 
                </tr> 
                </table> 
                </form> 
                    "; 
        }
    }   

}else{
    echo 'Could not connect at this time.';
}

if(isset($_POST['delete']))
    { 
    mysql_query("DELETE FROM `logs` WHERE `id` = '".$_POST['id']."' ");
    } 


?>

You have no action in your form. It looks like you want to submit to the same page.

<form action='THISPAGE.php' method='post'> 

Then just set the $_POST['id'] to a variable because MySQL has messed with me with arrays before. And redirect to another page after query, so that you aren't given a form of a non-existant id.

if(isset($_POST['delete']))
{ 
   $id = $_POST['id'];
   mysql_query("DELETE FROM `logs` WHERE `id` = '$id' ");
   header("Location: someOtherPage.php"); 
   exit(); //Exit to force redirect
} 

You may need to move the isset code to above the echo statement because you can't change the header after stuff has been echoed.

Just redirect to your page after delete or fetch the new data using select query again once you delete the data. Do something like this

if(isset($_POST['delete']))
{ 
    mysql_query("DELETE FROM `logs` WHERE `id` = '".$_POST['id']."' ");
    header("location:yourpage.php");
} 

I think that you need to move your delete code above the rows query

if(isset($_POST['delete']))
    { 
    mysql_query("DELETE FROM `logs` WHERE `id` = '".$_POST['id']."' ");
    } 

$entries = mysql_query("SELECT `id`,`date`, `location`, `description` FROM `logs` WHERE `username` = '$current_user'");

You are querying all the rows before you delete the row.

Also...

Please make note of Samitha's comment above.

I would recommend adding a 'deleted' column to your table, and adding a timestamp to the 'deleted' column when a row is "deleted", rather than deleting the entire row. Then, your query for would look something like this:

$entries = mysql_query("SELECT `id`,`date`, `location`, `description` FROM `logs` WHERE `username` = '$current_user' AND `deleted` != `NULL`");

Does your page refresh after the delete button is hit? Try adding refreshing & clearing of cache commands to your code.

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