PHP/MySQL删除用户-函数

PHP/MySQL删除用户-函数,php,mysql,forms,function,Php,Mysql,Forms,Function,我正试图更多地理解函数的工作,所以我决定为我的PHP脚本编写一些函数。第一个功能不起作用,我想知道为什么以后要避免这些问题 以下是我所做的: 我只是想创建一个函数来删除数据库中的管理员。这是我的密码: <?php // Delete Mitarbeiter function deleteMitarbeiter($id) { if(!isset($id) || !is_int($id)) { return FALSE; // wrong para

我正试图更多地理解函数的工作,所以我决定为我的PHP脚本编写一些函数。第一个功能不起作用,我想知道为什么以后要避免这些问题

以下是我所做的: 我只是想创建一个函数来删除数据库中的管理员。这是我的密码:

<?php
// Delete Mitarbeiter
  function deleteMitarbeiter($id) {
        if(!isset($id) || !is_int($id)) { 
            return FALSE; // wrong parameters, return early
        }
        $deleteUser = $this->db->prepare("DELETE FROM `admin` WHERE id=?");
        $deleteUser->bindParam(1, $id, PDO::PARAM_INT);
        return $deleteUser->execute(); // returns true on success, otherwise false
    }

if (isset ($_POST['deletemitarbeiter'])) {
    $member_Id =  $_POST['member_Id'];
    deleteMitarbeiter($member_Id);
    }
?>

<form method="post" action="edit_mitarbeiter.php" class="form-horizontal form-label-left">
<input type="hidden" value="<?php echo $result_member_ID;?>" name="member_Id">
<button type="submit" name="deletemitarbeiter" value="deletemitarbeiter" class="btn btn-danger btn-lg pull-right">Mitarbeiter löschen</button>
</form>
我确实有错误报告,但我没有收到任何错误。我想我的参数有问题吧?我真的坚持住了

编辑: 这是我编辑的代码:

<?php
// Delete Mitarbeiter
  function deleteMitarbeiter($id) {
        if(!isset($id) || !is_int($id)) {
            return FALSE; // wrong parameters, return early
        }
        try {
        $db = new DbContext();
        $deleteUser = $db->prepare("DELETE FROM `admin` WHERE id=?");
        $deleteUser->bindParam(1, $id, PDO::PARAM_INT);
        return $deleteUser->execute(); // returns true on success, otherwise false
    } catch (PDOException $e) {
        die("Could not execute the delete: " . $e->getMessage());
}}

if (isset ($_POST['deletemitarbeiter'])) {
    $member_Id = intval($_POST['member_Id']);
    deleteMitarbeiter($member_Id);
}
?>
现在我的错误消息是:致命错误:找不到类“DbContext”!我的代码应该是什么样子

mysqli\u real\u escape\u string返回一个字符串。因此is_int$id为false,并且该方法不执行

在解决此问题的方法中执行转义

更新


is_int函数检查实数类型,如果传递字符串,则返回false,即使字符串是数字。您可以使用is\u numeric来检查数字字符串。

这将使您朝着正确的方向前进

希望这有帮助

编辑


让我们深入了解一些基本知识。为什么不能在免费函数中使用$this:

<?php

//our base class
class Test
{
    private $privateVar = 0;

    public function __construct($value)
    {
        //access $this from within this class Test
        $this->privateVar = $value;
    }

    public function DoSomething()
    {
        //access $this from within this class Test with the initialized value from the constructor
        echo $this->privateVar;
    }

    public static function DoSomeStatic()
    {
        //since this is a static method you can't access $this here...
        echo "some static";
    }
}

//your function here
function testFunction()
{
    $test = new Test(1233);
    $test->DoSomething();

    $test2 = new Test(456677);
    $test2->DoSomething();

    Test::DoSomeStatic();

    //and you can't access $this here also, since $this is the context of the class.
    //i.e. if you aren't in a class there exists no object $this and therefor you can't call $this->DoSomething() here.
}

//and call your testfunction
testFunction();
//i suppose your db context looks something like this:
class DbContext
{
    private $db = NULL;

    public function __construct()
    {
         $this->db = new PDO($dsn, $user, $password);
    }
}
所以你不想在这个类之外访问$db。你必须修改它,或者做一些代理

如果您选择代理,请使用以下内容扩展您的类:

public function execute()
{
    $this->db->execute();
}
function deleteMitarbeiter($id)
{
   $context = new DbContext();
   $context->execute();
}
您可以在方法deleteMitarbeiter中创建上下文实例,如下所示:

public function execute()
{
    $this->db->execute();
}
function deleteMitarbeiter($id)
{
   $context = new DbContext();
   $context->execute();
}
但是,如果您选择使用DbContext作为基类,则可以在Mitarbeiter或同事的所有操作发生的地方扩展类

class DbContext
{
    protected $db;

    public function __construct()
    {
        $this->db = new PDO...
    }
}

class CoWorkerController extends DbContext
{
     function delete($id)
     {
        $this->db->execute();
     }
}

我在第一篇文章中编辑了代码并删除了转义字符串,但它仍然不起作用。我的方法应该是什么样的?我认为问题在于方法post传递的参数。通常,每个提交的值都是字符串,如果在那里使用它们,则必须将它们转换回int。试试$member\u Id=intval$\u POST['member\u Id'],你在你的函数中的什么地方告诉它在出现错误时死亡并将错误输出到浏览器?我看不出来。类似catch pdo异常$e{die无法在返回$deleteUser->execute;/之后执行删除:.$e->getMessage;}成功时返回true,否则返回false}@RaphaelMüller我使用了您的代码,现在收到以下错误消息:“致命错误:在'@ChristophC'中不在对象上下文中时使用$this。”$这仅在方法位于类中时可用。由于您的方法是一个免费函数,因此无法在那里访问$this。您必须实例化您的数据库类,即执行如下操作:$db=newdbcontext$db->prepare…@RaphaelMüller我编辑了我的第一篇文章,发布了我当前的代码。现在错误消息是:致命错误:找不到类“DbContext”!我做错了什么事。我的代码应该是什么样子?这不是我的问题,但谢谢!我得到的是错误,而不是空白页。@Kuya为什么你抓住了错误,然后就死了?你应该在那里做正确的错误处理…@RaphaelMüller如果你已经遇到了错误,为什么你希望脚本继续运行?@Kuya如果你没有捕捉到错误,它无论如何都会中断;我尝试了你的代码,但它不起作用。即使启用了错误报告,仍然没有收到错误消息,但用户未从数据库中删除。