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

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

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?

图片转代码服务由CSDN问答提供 功能建议

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

 &lt;?php 
 
class数据库{
 
公共函数getConnection(){
 $ result = false; 
尝试{
 $ result = new PDO('mysql:host = localhost; dbname = college  ','root',''); 
} catch(PDOException $ e){} 
返回$ result; 
} 
} 
 
 $ db = new Database(); 
 $ conn =  $ db-&gt; getConnection(); 
if(!$ conn){
die(“连接数据库时出错”); 
} 
 
?&gt; 
   \  n 
 

第二个脚本:

 &lt;?php 
require_once('../ config / Database.php'); 
?&gt; \  n 
 
&lt; form action =“”method =“post”&gt; 
&lt; label&gt;类别ID:&lt; / label&gt; 
&lt; input type =“text”name =“id”id =“id”  required =“required”占位符=“请输入ID”/&gt;&lt; br /&gt;&lt; br /&gt; 
&lt; input type =“submit”value =“删除”name =“delete”/&gt;&lt;  ; br /&gt; 
&lt; / form&gt; 
&lt; / div&gt; 
 
&lt;?php 
class类别{
 private $ conn; 
 priva  te $ id; 
 private $ name; 
 private $ description; 
 private $ created; 
 
 public function __construct($ db){
 $ this-&gt; conn = $ db; 
} \  n 
 / *此函数将类别ID作为
参数获取并从数据库中删除。* / 
 
公共函数deleteSelected($ ids){
 $ query ='DELETE FROM categories WHERE id ='  ; 
 
 if(is_array($ ids)){
 foreach($ ids as $ id)
 $ stmt = $ this-&gt; conn-&gt; prepare($ query) - &gt; execute($ query  。$ id); 
} 
其他{
 $ stmt = $ this-&gt; conn-&gt; prepare($ query) - &gt;执行($ query。$ ids); 
} 
} 
  n 
} 
?&gt; 
   
 
 

我的问题是 - 如何通过单击“删除”按钮调用“deleteSelected”功能,以便删除 给定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>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dswqw66280 2016-04-02 13:49

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

    if ( $_POST['id'] && $_POST['delete'] ) deleteSelected( (int) $_POST['id']  );
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题