I can not do update because it is not retrieving the id php pdo

0

Here is to recover in html :

if(isset($_GET["id"])){
        $administrador = new Administrador();        
        $mostrar = $administrador->listar_id($_GET["id"]);
  }

<form action="Crud.php" method="post" id="none">
            <input type="hidden" name="id" id="id" value="<?php echo $mostrar['id']?>">
            Nome:<input type="text" id="none" name="nome" value="<?php echo $mostrar['nome']?>">
            email:<input type="text" id="none" name="email" value="<?php echo $mostrar['email']?>">
            cpf:<input type="text" id="none" name="cpf" value="<?php echo $mostrar['cpf']?>">
            salario:<input type="text" id="none" name="salario" value="<?php echo $mostrar['salario']?>">
            <input type="submit" value="atualizar" name="atualizar">

Here is my file Crud.php, that binds html to the class.

if(isset($_POST["atualizar"])){// Chamada Método atualizar
                $administrador = new Administrador();
                $administrador->atualizar(strip_tags(trim($_POST['id'])),(strip_tags(trim($_POST['nome']))),(trim($_POST['email'])),(trim($_POST['cpf'])),(trim($_POST['salario'])));
            }

The atualizar method is working perfectly, what is working is not recovering id .

public function atualizar($id,$nome,$email,$cpf,$salario){
        $sql_atualizar = "UPDATE administrador SET nome='$nome', email='$email', cpf='$cpf', salario='$salario' WHERE id = '$id'";        
        try{
            $query_atualizar = $this->conn()->prepare($sql_atualizar);
            $query_atualizar->bindValue(':id',$id,PDO::PARAM_STR);
            $query_atualizar->bindValue(':nome',$nome,PDO::PARAM_STR);
            $query_atualizar->bindValue(':email',$email,PDO::PARAM_STR);
            $query_atualizar->bindValue(':cpf',$cpf,PDO::PARAM_STR);
            $query_atualizar->bindValue(':salario',$salario,PDO::PARAM_STR);
            $query_atualizar->execute();
            print_r($query_atualizar);
            //echo "<script>alert('Administrador alterado com sucesso! ')</script>";
            //header('location: Index.php');

        }catch(PDOException $err){
            echo " Erro: ".$err->getMessage();
        }
    }

How I posted it right up:

if(isset($_GET["id"])){
        $administrador = new Administrador();        
        $mostrar = $administrador->listar_id($_GET["id"]);
  }

In case, if I do that right, get the get id and put the id straight, but that's not what I want.

if(isset($_GET["id"])){
        $administrador = new Administrador();        
        $mostrar = $administrador->listar_id("10");
  } 

So he brings all the results of the id 10, summarizing what should I do to him to retrieve the id with Get?

This is my method list by id of my class:

public function listar_id($id){       
        try{
            $lista = ("SELECT * FROM administrador WHERE id = '$id'");
            $mostra_lista = $this->conn()->prepare($lista);
            $mostra_lista->bindValue(":id", $id,PDO::PARAM_INT);//pegando a id e substituindo por id
            $mostra_lista->execute();// Query e executado sempre que for uma lista comum, no caso da algo mais restrito e preferencial usar exec ou prepare

            //pegando só um valor

            if($mostra_lista->rowCount() == 1):// COntador
                return $mostra_lista->fetch(PDO::FETCH_ASSOC);
            endif;
        }catch(PDOException $err){
            echo " Erro: ".$err->getMessage();
        }
    }
    
asked by anonymous 21.12.2015 / 13:52

2 answers

0

Solution to my problem the script now works

if(isset($_GET["id"])){
   $id = filter_var($_GET["id"], FILTER_SANITIZE_NUMBER_INT);// Atribuindo a variável $id o valor inteiro no metodo get id
   $administrador = new Administrador();
   $id = $administrador->listar_id($id);
}
    
23.12.2015 / 17:32
0

Your problem is very simple:

Your form looks like this:

<form action="Crud.php" method="post" id="none">

In other words, it is using the POST method and in your PHP code you are trying to recover the id that would have been sent via the GET method

if(isset($_GET["id"])){
           |
           -- Deveria ser $_POST["id"]

Or your form should be:

<form action="Crud.php" method="get" id="none">

This is the most obvious problem of your code. Even changing what has been suggested you may encounter some more problems related to this exchange of HTTP methods. I recommend a read on this. The best links are in English, like the w3c , but since we are in Portuguese OS, read this article: How the get and post methods work

    
21.12.2015 / 16:39