通过单击按钮从 class 调用函数

Calling function from a class by clicking a button

我有这样的脚本来连接数据库:

<?php

class Database {

    public function getConnection() {
        $result = false;
        try {
            $result = new PDO('mysql:host=localhost;dbname=college', 'root', '');
        } catch(PDOException $e) { }
        return $result;
    }
}

$db = new Database();
$conn = $db->getConnection();
if (!$conn) {
die("Error connecting to the database");
}

?>

第二个脚本:

<?php
require_once('../config/Database.php');
?>


<form action="" method="post">
<label>Category id :</label>
<input type="text" name="id" id="id" required="required" placeholder="Please     Enter Id"/><br /><br />
<input type="submit" value=" Delete " name="delete"/><br />
</form>
</div>

<?php
class Category {
    private $conn;
    private $id;
    private $name;
    private $description;
    private $created;

    public function __construct($db) {
        $this->conn = $db;
    }

    /* This function will get the ids of categories as
    parameters and delete them from database.*/

    public function deleteSelected($ids) {
        $query = 'DELETE FROM categories WHERE id=';

        if (is_array($ids)) {
            foreach ($ids as $id)
                $stmt = $this->conn->prepare($query)->execute($query.$id);
        }
        else {
            $stmt = $this->conn->prepare($query)->execute($query.$ids);
            }
    }

}
?>

我的问题是 - 如何通过单击 "Delete" 按钮调用 "deleteSelected" 函数,以便删除具有给定 ID 的类别?

"require_once('../config/Database.php');"之后:

if ( $_POST['id'] && $_POST['delete'] ) deleteSelected( (int) $_POST['id']  );

首先记住一个网页的生命周期。

首先启动页面,除了使用默认数据构建页面外,您不想做任何其他事情。

然后用户输入一些数据并按下提交按钮,在这种情况下你想知道用户确实做了一些事情并且页面已经发送给你来处理他们所做的事情。

所以最好将 PHP 代码放在脚本的顶部和测试中,只有在提交页面进行处理时才为真,而不是在首次加载页面时单击 link 或菜单。

您的原始代码中也有很多错误,因此您最好先查看所有这些错误,然后再根据您注意到的第一件事更改代码。

<?php
require_once('../config/Database.php');

class Category {
    private $conn;
    private $id;
    private $name;
    private $description;
    private $created;

    public function __construct($db) {
        $this->conn = $db;
    }

    /* This function will get the ids of categories as
       parameters and delete them from database.*/

    public function deleteSelected($ids) {
        // notice I added a prameter to this query
        $query = 'DELETE FROM categories WHERE id=?';

        // one of the points of using a prepared query is that 
        // you can prepare it once and then execute it many time
        // with different paramters

        $stmt = $this->conn->prepare($query);

        if (is_array($ids)) {
            foreach ($ids as $id)
                $stmt->execute([$id]);
            }
        } else {
            $stmt->execute([$ids]);
        }
    }

}

// did user press submit
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // what button did they press
    if ( isset( $_POST['delete'], $_POST['id']) ) {
        // we have been request to delete
        // and we have an id to control the delete

        // instantiate the class we need
        $cat = new Category($conn);

        // clean up our input before we us it in a SQL query
        $id = filter_input ( INPUT_POST , 'id', FILTER_SANITIZE_NUMBER_INT );

        // call the method we want to run
        $cat->deleteSelected($id);
    }
}
?>


<form action="" method="post">
<label>Category id :</label>
<input type="text" name="id" id="id" required="required" placeholder="Please     Enter Id"/><br /><br />
<input type="submit" value="Delete" name="delete"/><br />
</form>
</div>