Problem when performing update

0

I have on the site the system to register, list and edit users.

Registering and Listing are ok, however, when trying to update a record, it does not change in DB and also does not return any errors. I already checked the connection and it's correct.

Here are the codes:

listar.php

<?php
include_once("conexao.php");

$result_usuario = "SELECT * FROM clientes";
$resultado_usuario = mysqli_query($conn, $result_usuario);
while ($row_usuario = mysqli_fetch_assoc($resultado_usuario)){
    echo $row_usuario['id_cli']."<br>";
    echo $row_usuario['nome_cli']."<br>";
    echo $row_usuario['cpf_cli']."<br>";
    echo $row_usuario['cnpj_cli']."<br>";
    echo $row_usuario['raz_cli']."<br>";
    echo $row_usuario['tel_cli']."<br>";
    echo $row_usuario['cel_cli']."<br>";
    echo $row_usuario['email_cli']."<br>";
    echo $row_usuario['senha_cli']."<br>";
    echo $row_usuario['nivel']."<br>";
    echo $row_usuario['ip_cli']."<br>";
    echo $row_usuario['data_cad_cli']."<br>"; ?>

<a href="editar.php?id=<?php echo $row_usuario['id_cli']; ?>">Editar</a>
<hr>

<?php }
?>

edit.php

    <?php
include_once("conexao.php");

$id = $_GET['id'];
$result_usuario = "SELECT * FROM clientes WHERE id_cli=$id";
$resultado_usuario = mysqli_query($conn, $result_usuario);
$row_usuario = mysqli_fetch_assoc($resultado_usuario);
?>

<form method="post" action="processa_edicao.php">
<input name="id_cli" type="number" readonly id="id" value="<?php echo $row_usuario['id_cli'] ?>">
<input name="nome_cli" type="text" required="required" id="nome" placeholder="Nome Completo" value="<?php echo $row_usuario['nome_cli'] ?>">
<input name="cpf_cli" type="number" required="required" id="cpf" placeholder="CPF" value="<?php echo $row_usuario['cpf_cli'] ?>">
<input name="cnpj_cli" type="number" id="cnpj" placeholder="CNPJ" value="<?php echo $row_usuario['cnpj_cli'] ?>">
<input name="raz_cli" type="text" id="razao_social" placeholder="Razão Social" value="<?php echo $row_usuario['raz_cli'] ?>">
<input name="tel_cli" type="tel" id="telefone" placeholder="(99)9999-9999" value="<?php echo $row_usuario['tel_cli'] ?>">
<input name="cel_cli" type="tel" id="celular" placeholder="(99)99999-9999" value="<?php echo $row_usuario['cel_cli'] ?>">
<input name="email_cli" type="email" required="required" id="email" placeholder="E-mail" value="<?php echo $row_usuario['email_cli'] ?>">
<input name="senha_cli" type="text" required="required" id="senha" placeholder="Senha" value="<?php echo $row_usuario['senha_cli'] ?>">
<select name="nivel" required id="nivel">
  <option value="1">Selecione</option>
  <option value="1">Básico</option>
  <option value="2">Profissional</option>

  <input name="ip_cli" type="number" readonly id="ip" value="<?php echo $row_usuario['ip_cli'] ?>">
  <input name="data_cad_cli" type="datetime" readonly id="data_cad" value="<?php echo $row_usuario['data_cad_cli'] ?>">

  <input type="submit" value="Editar">
</select>
</form>

processa_edicao.php

<?php
$nome = $_POST['nome_cli'];
$cpf = $_POST['cpf_cli'];
$cnpj = $_POST['cnpj_cli'];
$razao_social = $_POST['raz_cli'];
$telefone = $_POST['tel_cli'];
$celular = $_POST['cel_cli'];
$email = $_POST['email_cli'];
$senha = $_POST['senha_cli'];
$nivel_id = $_POST['nivel'];

$id = $_POST['id'];

include_once("conexao.php");

$result_usuario = "UPDATE clientes SET nome_cli = '$nome', cpf_cli = '$cpf', cnpj_cli = '$cnpj', raz_cli = '$razao_social', tel_cli = '$telefone', cel_cli = '$celular', email_cli = '$email', senha_cli = '$senha', nivel = '$nivel_id'  WHERE id_cli = '$id'";
$resultado_usuario = mysqli_query($conn, $result_usuario) or die(mysqli_error($conn));

header("location: listar.php");
?>

connection.php

<?php
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "cadastro_usuarios";

//Criar a conexao
$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);

if(!$conn){
    die("Falha na conexao: " . mysqli_connect_error());
}else{
    //echo "Conexao realizada com sucesso";
}
?>
    
asked by anonymous 27.04.2017 / 22:13

1 answer

2

1: When you have it done

"UPDATE clientes SET nome_cli = '$nome' WHERE id_cli = '$id'";

You are only updating the name.

To update all received fields via post has to look like this:

$result_usuario = "UPDATE clientes SET nome_cli = '$nome', cpf_cli = '$cpf', cnpj_cli = '$cnpj', raz_cli = '$razao_social', tel_cli = '$telefone', cel_cli = '$celular', email_cli = '$email', senha_cli = '$senha', nivel = '$nivel_id'  WHERE id_cli = '$id'";

2: On the edit.php page in <input name="id_cli" type="number" disabled id="id"

Switch disabled with readonly

  

Disabled does not pass the value to the form, in addition to being unable to edit.

     

Readonly sends the value to the form and also can not edit.

If you create a form in html and set some input field with the property disabled, know that submitting PHP will not receive this value!

It's as if the field / variable did not even exist! And this goes for any other form element that has defined the disabled property in your code.

    
27.04.2017 / 22:49