用php删除行

i currently have a SQL Database where i store some data. That data is getting viewed on a website with the following code:

 echo "<tr><td>" . $row["project"]. " " . "</td><td>" . "  ". $row[ "project_desc"]. " " . "</td><td>" . "  ".  $row["hours"]. "</td><td>" . "  ".  $day. "</td></tr>";

But now i want it to have a extra tab where you can delete the row that is showed on the website from the database. How can i do that? if you need it, i use MySQL And im using mysqli

You should add this code inside your row-

<td><a href="delete.php?id=<?echo $row['id'];?>">Delete</a></td>

when user clicks "Delete" then delete.php is called, so purchase id which want to be deleted should be transfer via delete.php,

delete.php

$id = $_GET['id'];
mysql_query("DELETE from purchase WHERE id='$id'");

Note: This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used along with prepared statements.

Hence i am suggesting you to learn the PDO tutorial from this link and this is very useful for developing in the future projects.

To delete a single row in a table, you use the DELETE statement with a WHERE clause that specifies which row to delete.

Example for the MySQL Delete: http://www.phpeasystep.com/mysql/7.html

Delete Example using mysql:

  1. In the list page where you display the data you need to add up the code for sending it to the delete.php page or you can delete it in the same page where you list the data.
  2. Below i will provide you with two methods of how to pass the data.

Method One: Same page delete

<td><a href="?id=<?echo $row['id'];?>">Delete</a></td>

Method Two: Delete.php page delete

<td><a href="delete.php?id=<?echo $row['id'];?>">Delete</a></td>

PHP code to delete the data from the table:

If you need to delete the data from the list page itself or you are going to delete in the delete.php page you need to follow up the same code to get the REQUEST from the URL.

<?php
if(isset($_REQUEST['id']))
{
$id = $_REQUEST['id'];
$query="DELETE FROM TABLENAME WHERE `id`='".$id."'";
$execute = mysql_query($query);
echo "Data Delete Successfully";
}
?>

If you are going to delete in the delete.php page you need to connect the database.php file into that page.

delete.php

<?php
include('database.php');
//Followed by Delete Code over Here.
// And then you have to redirect the page using the header location
header('Location: BACK TO YOUR LIST PAGE');
?>

Hope so my explanation will be clear for your understanding purpose and you can get it make working on the next projects on the fly.

Delete Example using PDO:

<?php

/**
 * PHP MySQL Delete Data Demo
 */
class DeleteDataDemo {

    const DB_HOST = 'localhost';
    const DB_NAME = 'classicmodels';
    const DB_USER = 'root';
    const DB_PASSWORD = '';

    /**
     * PDO instance
     * @var PDO 
     */
    private $pdo = null;

    /**
     * Open a database connection to MySQL
     */
    public function __construct() {
        // open database connection
        $conStr = sprintf("mysql:host=%s;dbname=%s", self::DB_HOST, self::DB_NAME);
        try {
            $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD);
        } catch (PDOException $e) {
            die($e->getMessage());
        }
    }

    /**
     * Delete a task based on a specified task id
     * @param int $id
     * @return bool true on success or false on failure
     */
    public function delete($id) {

        $sql = 'DELETE FROM tasks
                WHERE task_id = :task_id';

        $q = $this->pdo->prepare($sql);

        return $q->execute([':task_id' => $id]);
    }

    /**
     * close the database connection
     */
    public function __destruct() {
        $this->pdo = null;
    }

}

$obj = new DeleteDataDemo();
// delete id 2
$obj->delete(2);

Here i have found a very good tutorial for learning purpose please learn using this link.