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.
– Cobra
A question: why, when editing, you only change the name, but store all incoming variables in $_POST?
– Cobra
$result_usuario = "UPDATE clients SET name_cli = '$name', cpf_cli = '$Cpf', ........ WHERE id_cli = '$id'";
– user60252
Solution in the edited answer
– user60252
You can edit your question with the changes made?
– user60252
I changed the question, Leo.
– Turkish