Problem while doing update

Asked

Viewed 82 times

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 is not changed on DB and also does not return any error. I already checked the connection and it is correct.

Follow the codes:

list.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>

php processing.

<?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");
?>

php connection.

<?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";
}
?>
  • There are some problems and also possible improvements in your code, but let’s start from the basics. Change the line "$resultado_usuario = mysqli_query($Conn, $result_usuario);" to "$resultado_usuario = mysqli_query($Conn, $result_usuario) or die(mysqli_error($Conn);", rotate and post the result here.

  • A question: why, when editing, you only change the name, but store all incoming variables in $_POST?

  • $result_usuario = "UPDATE clients SET name_cli = '$name', cpf_cli = '$Cpf', ........ WHERE id_cli = '$id'";

  • Solution in the edited answer

  • You can edit your question with the changes made?

  • I changed the question, Leo.

Show 1 more comment

1 answer

2

1: When you order to do

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

is just updating the name.

To update all fields received via post has to be 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.php edit page <input name="id_cli" type="number" disabled id="id"

swap the disabled for readonly

Disabled does not pass the value to the user, and cannot edit.

Readonly sends the value to the form and also cannot edit.

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

It’s like the field/variable doesn’t even exist! And that goes for any other form element that has set the disabled property in its code.

  • Leo, it would be nice if you could complement your answer by making it clear to the user why it’s not working, why it’s done in the way you mentioned, etc... Giving the answer "hand in hand" to the user even solves the question, but does not add much to the community itself.

  • Wow, while typing you added. Great!

  • Transmimento de pensação, rs

  • Leo eThiago. I didn’t insert all the fields in UPDATE in order to test only. But I made the changes as they passed me but it didn’t work. &#xA;&#xA; $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'";&#xA; $resultado_usuario = mysqli_query($Conn, $result_usuario) or die(mysqli_error($Conn));

  • Leo. I made the change as you said changing disable for readonly. But nothing yet. I redid all the code again using your information and that of Thiago. Only that there was no change.

  • Leo. I remade the whole process by changing it with the information you gave me. Nothing yet. It will be the version?

  • Version of what? PHP? my version is PHP Version 5.4.43

  • edit your question with the already modified files

Show 3 more comments

Browser other questions tagged

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