Redirect by href PHP

Asked

Viewed 271 times

1

I have a DAO class that contains all the methods to execute. But when the user enters his access, he makes a select of the bank bringing his customers.

Inside the while I put

My question is: how will I call the DAO class and the delete function it contains in it?

Example I’ve done and it didn’t work

echo "<td><a href='UsuarioDAO.php?excluir=$id'><button>Clique</button></td> </a>";

class UsuarioDAO(){

public function excluir(){
//codigo vem aqui
}

}

  • I think we need to get the value passed through the URL via get....

  • 1

    Another detail is that there is a syntax error in html....

1 answer

3

In the archive User.php, put something like this:

class UsuarioDAO(){

    public function excluir($id){
        //codigo vem aqui
        echo 'ID para excluir: '.$id;
    }

}

/*
Verifica o parâmetro recebido pela URL
*/
$method = 'excluir';
if (isset($_GET[$method])) {
    $id = trim(strip_tags($_GET[$method]));
}

/*
Instancia a classe e invoca o método se existir
*/
$c = new UsuarioDAO;
if (method_exists($c, $method)) {
    $c->{$method}($id);
} else {
    echo 'Método não existe: '.$method;
}
  • I believe that the person who denied his answer did so because of this line: $method = 'delete'; Take that out which is a good answer. I gave a +1 for your willingness to help the guy and to counteract the negative vote although I would never make a link directly access the DAO. He should call a controller and he should call.

  • Reminding the author of the question that one should use a PDO Prepared statement or mysqli real escape string to clear the id variable and pass the id value to the sql query securely.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.