user1804766
user1804766

Reputation:

Reload Page After Delete in PHP

I have made this function to delete the record from the table. When the delete operation is done, the page displays all data(including the deleted one), But, I need to refresh or reload the page to see the results after I have deleted the row. How it can be done in following code? Thanx in advance!!

            public function deletedata(){

        if(isset($_GET['del_id'])){

            $delete_id = $_GET['del_id'];

            $query ="DELETE FROM tbl_data WHERE project_id ='".$delete_id."' ";

            $this->databaseObject->getConnection()->query($query);
    }

Upvotes: 1

Views: 8614

Answers (2)

showdev
showdev

Reputation: 29178

As mentioned in laurencek's alternative, there's probably no need to reload the page. Just perform any necessary record deletion before rendering the HTML.

<?php

$delete_id=isset($_GET['del_id'])&&is_numeric($_GET['del_id'])?$_GET['del_id']:false;
if ($delete_id) {
  $query ="DELETE FROM tbl_data WHERE project_id ='".$delete_id."' ";
  $this->databaseObject->getConnection()->query($query);
}
$query ="SELECT * FROM tbl_data WHERE 1;";
$this->databaseObject->getConnection()->query($query);

?>
<html>

// some php/html to loop through the query and display records

</html>

This assumes your IDs are numeric. If not, I strongly suggest checking/escaping the variable before introducing it to your database.

Upvotes: 0

Coin_op
Coin_op

Reputation: 10728

Usually you'd use the header function to reload a page.

Something like this should work: header('Location: '.$_SERVER['REQUEST_URI']);

http://php.net/manual/en/function.header.php

Alternatively, depending on the structure of your code you could simply call the delete code before the code that retrieves the records. That way you'd avoid the need to reload the page.

Upvotes: 3

Related Questions