doupeng2253 2016-04-02 13:41
浏览 68
已采纳

通过单击按钮从类中调用函数

I've script like this to connect with database:

<?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");
}

?>

And the second script:

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

}
?>

My question is - how can I call the "deleteSelected" function by clicking "Delete" button, so it deletes the category with given id?

  • 写回答

2条回答 默认 最新

  • donte1234567 2016-04-02 14:14
    关注

    First remember the life cycle of a web page.

    First you launch the page and you dont want to do anything other than build the page with its default data.

    Then the user enters some data and presses the submit button, in this case you want to know that the user actually did something and the page has been sent to you to deal with what they did.

    So its best to put you PHP code at the top of the script and inside a test that will only be true when a page is submitted for processing, and not when the page is first loaded fron a click on a link or menu.

    Also there are lots of errors in your original code so you had better look at all of this before changing your code with the first thing you notice.

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度