在我的 Controller 中: private function verwijderAction() -6ren">
gpt4 book ai didi

php - 从 MySQL 中删除 id

转载 作者:行者123 更新时间:2023-11-29 22:23:14 26 4
gpt4 key购买 nike

在MySQL中我有几个用户,当我想删除一个用户时我使用以下代码:

<td><b><a href='.?control=directeur&action=verwijderGebruiker&id=".$leerling->getId()."'><img src='img/delete.png' /></a></b></td>

在我的 Controller 中:

 private function verwijderAction()
{
$this->model->verwijderGebruiker();
$this->forward('default','directeur');
}

在我的模型中:

 public function verwijderGebruiker()
{
$id = filter_var($_REQUEST['id'], FILTER_VALIDATE_INT);

if($id!=false)
{
$sql = 'DELETE FROM `contacten` WHERE `id`=:id';
$stmnt = $this->db->prepare($sql); // bereid de query voor
$stmnt->bindParam(':id',$id); // bindParam = verbindt de parameter: ":<parameter>" met de "<variable>".
$stmnt->execute(); // voert de query uit
}
}

当我想删除 ID 时,它会在 URL (action=verwijderGebruiker&id=5) 中显示正确的内容,但它不会删除它,而是返回主页。

最佳答案

你的id永远不会进入你的方法。你必须将它作为参数传递给你的函数:

public function verwijderGebruiker($id)
{
$id = filter_var($id, FILTER_VALIDATE_INT);
// ....
}

//call it outside your class with your request-variable:
$my_class = new MyClass();
$my_class->verwijderGebruiker($_REQUEST['id']);

关于php - 从 MySQL 中删除 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30525244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com