I can’t place a database data in a value of an input from my form

Asked

Viewed 44 times

2

This is a CRUD in php where the only option missing is the editing of a register, it happens that I intend to use the same form The problem is that when I click edit the fields of value inputs remain empty which is the value I set by default. Let’s go to code:

Formulário para cadastro com a tabela dos cadastrados

Inputs from the page above:

<input type="text" name="nome" class="form-control" value="<?php echo $nome?>" maxlength="70">
<input type="password" name="senha" class="form-control" value="<?php echo $senha?>" maxlength="25">

Edit button link: (NOTE: your file is not in quotes because everything is already being treated as a string)

<a class='btn btn-info' href='processaDados.php?editar=$row[idusuario]'>Editar</a>

Code of the processed fileDados.php that is already executed at the time the page is loaded:

session_start();

$conn = new PDO("mysql:dbname=db_CRUD;host=localhost", "root", "");

$nome = '';
$senha = '';

Code of the processed fileDados.php that runs when the Edit button is triggered:

if (isset($_GET['editar'])) {

    $stmt = $conn->prepare("SELECT * FROM tb_dados WHERE idusuario = :ID");
    $stmt->bindParam(":ID", $id);
    $id = (int) $_GET['editar'];
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $nome = $results[0]["desnome"];
    $senha = $results[0]["dessenha"];

    header("location: index.php");
}  

I only put the main parts of the code to be simplified and because I know that the problem is in this section above. It is worth noting that I have already tested the last code outside the if and print the amounts received on processaDados.php (I removed the header("location: index.php");), I mean, I’m getting the name and password when I click edit, but the problem is when I try to assign these values to values of inputs.

1 answer

0


Wow I was already so close to the answer. Just remove the header("location: index.php"); why in putting this line of code I was redefining the value of $nome and $senha the emptiness, because the $_GET['editar'] did not exist again. Also as needed to keep in the current page I changed the link Edit <a class='btn btn-info' href='processaDados.php?editar=$row[idusuario]'>Editar</a> for <a class='btn btn-info' href='?editar=$row[idusuario]'>Editar</a>

Browser other questions tagged

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