How to execute a function from a url?

2

If I leave the code just below the inside of the funcoes.php file of my system, when the page reloads, it deletes the post that I had deleted.

  $tipo   = $_GET['tipo'];
  $funcao = $_GET['funcao']; 

  if($_GET['tipo'] == 'blog' && $_GET['funcao'] == 'excluir'){

    require("classes/Database.php");
    $pdo  = Database::connect();
    $pdo->query("DELETE FROM tb_blog WHERE ID = ".$_GET['id']."");

    echo "
    <META HTTP-EQUIV=REFRESH CONTENT = '0;URL='>
    <script type=\"text/javascript\">
      window.location = \"index\";
    </script>
    "; 

  }

Now if I put this code inside a function , it does not work.

  function excluirDados(){
    $tipo   = $_GET['tipo'];
    $funcao = $_GET['funcao']; 

    if($_GET['tipo'] == 'blog' && $_GET['funcao'] == 'excluir'){

      require("classes/Database.php");
      $pdo  = Database::connect();
      $pdo->query("DELETE FROM tb_blog WHERE ID = ".$_GET['id']."");

      echo "
      <META HTTP-EQUIV=REFRESH CONTENT = '0;URL='>
      <script type=\"text/javascript\">
        window.location = \"index\";
      </script>
      "; 

    }  
  }

Question : Why the same code but inside a function does not work the same as the loose code inside the file?

I'm passing this url: .../index&tipo=blog&funcao=excluir&id=40

    
asked by anonymous 09.11.2015 / 14:56

1 answer

2

You can try this:

require("classes/Database.php"); /* requiro fora da função */
function excluirDados(){
    $tipo   = $_GET['tipo'];
    $funcao = $_GET['funcao']; 

    if($_GET['tipo'] == 'blog' && $_GET['funcao'] == 'excluir'){


      $pdo  = Database::connect();
      $pdo->query("DELETE FROM tb_blog WHERE ID = ".$_GET['id']."");

      echo "
      <META HTTP-EQUIV=REFRESH CONTENT = '0;URL='>
      <script type=\"text/javascript\">
        window.location = \"index\";
      </script>
      "; 

    }  
  }
excluirDados(); /* chamar a função (no seu exemplo ela não estava sendo chamada */
    
04.06.2016 / 16:58