PHP / MySQL删除用户 - 功能

时间:2015-09-03 08:07:39

标签: php mysql forms function

我试图更多地了解函数的工作,所以我决定为我的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>

我确实有error_reporting,但我没有收到任何错误。我猜我的参数有问题吗?我真的坚持这个。

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

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

现在我的错误消息是:Fatal error: Class 'DbContext' not found!我的代码应该是什么样的?

3 个答案:

答案 0 :(得分:0)

mysqli_real_escape_string返回一个字符串。所以is_int($id)为false,方法不执行。

在方法内部进行转义以解决此问题。

<强>更新

is_int函数检查实际类型,如果传递字符串,则返回false,即使字符串是数字。您可以使用is_numeric()来检查数字字符串。

答案 1 :(得分:0)

这应该让你朝着正确的方向前进。

希望这有帮助。

<强> EDITED

<?php
$host = 'localhost'; $db = 'database-name'; $user = 'database-user'; $pw = 'database-password';
$conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user, $pw);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Delete Mitarbeiter
  function deleteMitarbeiter($id) {
        if(!isset($id) || !is_int($id)) { 
            return FALSE; // wrong parameters, return early
        }
        try {
        $id = $_POST['member_Id']; // this is the line I added
        $sql = "DELETE FROM `admin` WHERE id=?";
        $deleteUser = $conn->prepare($sql);
        $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 =  $_POST['member_Id'];
    deleteMitarbeiter($member_Id);
}
?>

答案 2 :(得分:0)

让我们深入了解一些基础知识。为什么你不能在免费功能中使用$ 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();

因为您的方法deleteMitarbeiter是一个自由函数(在类上下文中不存在),所以没有$this个对象。所以,你必须创建一个对象的实例或使用静态方法。

BTW:我还会考虑在编程时只使用英语单词,而不是德语单词......

现在解决你的问题:

您希望在此免费功能中使用您的数据库上下文:

//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。你必须修改它,或做一些proxiing。

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

public function execute()
{
    $this->db->execute();
}

您可以在方法deleteMitarbeiter中创建上下文的实例,如下所示:

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