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();
}
}
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.
– rray