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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?