I cannot update because it is not recovering php Pdo id

Asked

Viewed 170 times

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, who connects the html with 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 method atualizar is working perfectly, what is giving dick is that is not recovering the 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();
        }
    }

Like I just posted up:

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

In case, if I do it right, take 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 it brings all the results of id 10, summarizing what I must do to get the id back 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();
        }
    }
  • 1

    Apart from the fact that you are passed the variables directly in the statement(treat this later), can’t you know where the id comes from, it comes from a link? put this information in the question.

2 answers

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 which 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">

That’s the most obvious problem in your code. Even changing what was suggested you may encounter some more problems related to this exchange of HTTP methods. I recommend reading about it. The best links are in English, as the w3c, but since we are in the Portuguese OS, read this article: How get and post methods work

  • $_GET["id"] maybe it was set in a link, so the check.

  • @rray Yeah, maybe you’re right, but by the code provided you can’t be sure. At least he may have an idea of what might be wrong.

  • 1

    Thank you, but after I’ve broken my head I’ve managed to, which is why I love programming, these headaches that make this profession so fascinating. I had already tried this but tbm gave error, actually I just needed to assign in variable. So it was like this.

  • 1

    if(isset($_GET["id"]){ $id = filter_var($_GET["id"], FILTER_SANITIZE_NUMBER_INT);// Assigning the variable $id the integer value in the get id&#Xa method; $administrator = new Administrator(); $id = $administrator->listar_id($id); }

  • i had done so if(isset($_GET["id"])){ $administrator = new Administrator(); $show = $administrator->listar_id($_GET["id"]); }

  • 1

    @Alisonpaulo Good that solved! You should add your solution as a response and mark as correct!

Show 1 more comment

0


Solution of 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);
}

Browser other questions tagged

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