1
I made a code that updates the data entered by the user in the database, however, it is giving the message that the data has been updated, but does not update anything. The system works as a search. The user enters the number (number and primary key) and then opens a modal which contains the data of that number. By clicking on button "edit", the user has the option to edit the "employee", "rca" and "region", and then save. When you click "save" I need it to update the database.
Follows the code where the inputs:
<?php
include_once("../conn/conexao.php");//faz a conexao com o banco de dados
if(!empty($_POST['numerodigitado'])){
$numerodigitado = $_POST['numerodigitado'];
$result = "SELECT * FROM tb_numeros WHERE numero = '$numerodigitado' ";
$resultado = mysqli_query($conexao, $result);
$row = mysqli_fetch_assoc($resultado);
if($resultado -> num_rows > 0){
echo"
<div class='modal fade' id='squarespaceModa2' tabindex='-1' role='dialog' aria-labelledby='modalLabel' aria-hidden='true'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'><span aria-hidden='true'>×</span><span class='sr-only'>Close</span></button>
<div class='modal-body'>
<form method='post' action='atualizar.php'>
<div class='form-group'>
<div class='col-xs-12'><center>
<label for='exampleInputPassword1'>Funcionário</label></center>
<input type='text' name='funcionario' class='form-control' id='exampleInputPassword1' value=".$row['funcionario']." style='text-align: center;' readonly='readonly' >
<br></div>
</div>
<div class='form-group'>
<div class='col-xs-4'><center>
<label for='exampleInputPassword1'>Número</label></center>
<input type='text' name='numero' class='form-control' id='exampleInputPassword1' value=".$row['numero']." style='text-align: center;' readonly='readonly' >
</div>
</div>
<div class='form-group'>
<div class='col-xs-2'><center>
<label for='exampleInputPassword1'>RCA</label></center>
<input type='text' name='rca' class='form-control' id='exampleInputPassword1' value=".$row['rca']." style='text-align: center;' readonly='readonly' >
</div>
</div>
<div class='form-group'>
<div class='col-xs-6'><center>
<label for='exampleInputPassword1'>Região</label></center>
<input type='text' name='regiao' class='form-control' id='exampleInputPassword1' value=".$row['regiao']." style='text-align: center;' readonly='readonly' >
<br></div>
</div>
<div class='form-group'>
<div class='col-xs-6'><center>
<label for='exampleInputPassword1'>Número Chip</label></center>
<input type='text' name='nchip' class='form-control' id='exampleInputPassword1' value=".$row['nchip']." style='text-align: center;' readonly='readonly' >
</div>
</div>
<div class='form-group'>
<div class='col-xs-6'><center>
<label for='exampleInputPassword1'>IMEI</label></center>
<input type='text' name='imei' class='form-control' id='exampleInputPassword1' value=".$row['imei']." style='text-align: center;' readonly='readonly' >
<br></div>
</div>
<div class='form-group'>
<div class='col-xs-12'><center>
<label for='exampleInputPassword1'>Cadastrado Por</label></center>
<input type='text' name='usuario' class='form-control' id='exampleInputPassword1' value=".$row['usuario']." style='text-align: center;' readonly='readonly' >
<br></div>
</div>
<div class='form-group'>
<div class='col-xs-6'><center>
<button type='button' class='btn btn-default btn-lg btn-block' role='button' id='btnEditar' ><span class='glyphicon glyphicon-pencil'></span> Editar</button>
</center></div>
</div>
<div class='form-group'>
<div class='col-xs-6'><center>
<button class='btn btn-default btn-lg btn-block' role='button' type='submit' value='Cadastrar' name='Salvar'><span class='glyphicon glyphicon-ok'></span> Salvar</button>
</center></div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){ $('#squarespaceModa2').modal(); });
</script>";
}
else {
echo "<script>
alert('Número não encontrado.');
window.location='index.php';
</script>";
}
}
Now the code of atualizar.php
:
<?php
session_start();
include("../conn/conexao.php");
$numero = $_GET['numero'];
$funcionario= $_POST['funcionario'];
$rca= $_POST['rca'];
$regiao= $_POST['regiao'];
$nome=$_SESSION['nome'];
$up = mysqli_query($conexao, "UPDATE tb_numeros SET funcionario='$funcionario', rca='$rca', regiao='$regiao', usuario='$nome' WHERE numero = '$numero' ")or die (mysqli_error($conexao));
if($up):
echo "<script>
alert('Alterado com sucesso.');
window.location='index.php';
</script>";
else:
echo "<script>
alert('Ocorreu um erro ao atualizar, entre em contato com o administrador.');
window.location='index.php';
</script>";
endif;
Observing: have a script that makes them free inputs:
<script language='JavaScript'>
$("#btnEditar").on('click', function() {
$('input[name="funcionario"]').removeAttr('readonly');
$('input[name="rca"]').removeAttr('readonly');
$('input[name="regiao"]').removeAttr('readonly');
});
</script>
How to edit fields with attribute
readonly='readonly'
?– user60252
@Leocaracciolo then forgot to inform I have a script that when clicking the edit button the inputs are released: <script language='Javascript'> $("#btnEditar"). on('click', Function() { $('input[name="employee"]').removeAttr('readonly'); $('input[name="rca"]').removeAttr('readonly'); $('input[name="regiao"]').removeAttr('readonly'); }); </script>
– Ferb
Now yes, understood!
– user60252
@Leocaracciolo I edited there in the question also
– Ferb
Great, and how you’re passing variables to update.php?
– user60252