2
In my system has a registration page and a page with a table with the information of registered. In this table there is an edit button that links with an equal form of the registration page.
What should happen:
When clicking the save button, the modified fields should be changed in the record.
What’s going on:
When I try to change the record appears the alert
"successfully saved" and back to the previous page, as it should. But looking at phpMyAdmin, the record remains unchanged
The connection:
<?php
$connection = mysqli_connect("localhost", "root", "", "db_formacao");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Editing form:
<?php
require 'conn.php';
$queryColaboradores = mysqli_query($connection, "SELECT FORMACAO FROM participantes");
$turma = filter_input(INPUT_POST, 'TURMA');
$formacao = filter_input(INPUT_POST, 'FORMACAO');
$colaborador = filter_input(INPUT_POST, 'COLABORADOR');
$Realizado = filter_input(INPUT_POST, 'REALIZADO');
$id = filter_input(INPUT_POST, 'ID');
var_dump($queryColaboradores);
?>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h1 style="
margin-top:100px;">Inscrição</h1>
<p> </p>
<p class="lead"></p>
<ul class="list-unstyled">
<form id="cadastro" method="post" action="banco/updateEdicao.php" style="
text-align: left;
margin-top:50px;">
<fieldset disabled>
<div class="col-lg-12">
<div class="form-group" style="
text-align: left;">
<label for="FORMACAO">Formação: </label>
<input type="text" required class="form-control" id="FORMACAO" name="FORMACAO" value="<?php echo $formacao; ?>">
</div>
</div>
</fieldset>
<fieldset disabled>
<div class="col-lg-12">
<div class="form-group" method="post" style="
text-align: left;">
<label for="TURMA">Turma: </label>
<input type="text" required class="form-control" id="TURMA" name="TURMA" value="<?php echo $turma; ?>">
</div>
</div>
</fieldset>
<fieldset disabled>
<div class="col-lg-12">
<div class="form-group" method="post" style="
text-align: left;">
<label for="TURMA">Colaborador: </label>
<input type="text" required class="form-control" id="COLABORADOR" name="COLABORADOR" value="<?php echo $colaborador; ?>">
</div>
</div>
</fieldset>
<fieldset disabled>
<div class="col-lg-12">
<div class="form-group" method="post" style="
text-align: left;">
<label for="TURMA">ID participante: </label>
<input type="text" required class="form-control" id="PARTICIPANTE" name="PARTICIPANTE" value="<?php echo $id; ?>">
<input type="hidden" name="id" value="<?php echo $id ?>" />
</div>
</div>
</fieldset>
<div class="col-lg-12">
<fieldset disabled>
<div class="form-group">
<label for="previsto">Status</label>
<input type="text" id="PREVISTO" name="PREVISTO" class="form-control" value="Previsto">
</div>
</fieldset>
</div>
<div class="col-lg-12">
<div class="form-group" style="
text-align: left;">
<label for="REALIZADO">Realizado: </label>
<input type="text" required class="form-control" id="REALIZADO" name="REALIZADO" value="Realizado">
</div>
</div>
<div class="">
<button type="submit" class="btn btn-primary btn-lg btn-block">Salvar</button>
</div>
</form>
</ul>
</div>
</div>
</div>
The update:
<?php
$previsto = filter_input(INPUT_POST, 'PREVISTO');
$realizado = filter_input(INPUT_POST, 'REALIZADO');
$id = filter_input(INPUT_POST, 'ID');
$strcon = mysqli_connect('localhost', 'root', '', 'db_formacao') or die('Erro ao conectar ao banco de dados');
$sql = " UPDATE participantes SET REALIZADO = '$realizado' WHERE ID = '$id' ";
mysqli_query($strcon,$sql) or die("Erro ao tentar atualizar registro. " . mysqli_error($strcon));
mysqli_close($strcon);
echo '<script type="text/javascript">
alert("Salvo com Sucesso !");
window.history.go(-1);
</script>';
var_dump($id)
?>
This column of the PAID bank and this id are what kind of field? Varchar? If yes missing a single quote
– Bruno Folle
The ID is an auto_increment int, whereas the REALIZED is a varchar
– Mariana Bayonetta
Now it appears that Alert with the "saved successfully" warning redirects to the previous page as it should be, but when I open phpmyadmin the record has not been changed.
– Mariana Bayonetta
Have you tried to echo $sql generated to see if this right and even run it in hand to be sure
– Otto
I even gave a var_dump($forecast, $realized, $id) gave NULL NULL NULL. I gave a no sql too and gave the line of code that sql receives but without the ID
– Mariana Bayonetta
So that’s why. Your Where clause is coming with the empty value. You are saying to your mysql: "Update the DONE column where the id is equal to nothing". As you have no record where the id is equal to nothing it has not changed anything.
– Bruno Folle
Yes, but now I’ve introduced the ID, but it’s still not updating. I gave another var_dump($id) and the answer is still NULL.
– Mariana Bayonetta
But if you changed in $sql for $ID, you have to change everywhere too, anything for $id you have to change to $ID
– Lucas de Carvalho
Not the $id but the ID. 'ID'=$id
– Mariana Bayonetta