Update does not update database records

Asked

Viewed 707 times

1

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

inserir a descrição da imagem aqui

OBS: If I take the WHERE ID = '$id' all records are updated normally, but with WHERE not.

  • Have you seen if you’re even getting the ID value on $id = filter_input(INPUT_POST, 'ID'); ? Sometimes the variable $id is coming nulla.

  • Yes, I gave a var_dump and is getting NULL

  • By your code, every time you call this page update it will appear Alterado com sucesso, I suggest you do a check to see if it is actually being changed in the bank using mysqli_affected_rows($con);, I also suggest that you remove the window.history.go(-1); to stay on the page and you can see the error.

  • I’m checking if the record is being changed by phpMyAdmin, I already took the window.history.go(-1) to see the error but only print the var_dump.

  • Disabled does not pass the value to the user, and cannot edit. <fieldset disabled>

  • Yes, not being able to edit is exactly what I want for the fields that are with disabled, which are the fields that were filled in at the time of registration. Only one field is available for editing, which is the Realized.

  • I have a disabled capo that is getting the value of the participation ID to be edited.

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

  • I’ll put an image of my form.

  • I’ll put the link to another question of mine about another problem in the same form that has to do with what you’re saying. Disabled fields should be receiving a value imported from the bank and are not receiving.

  • https://answall.com/questions/234124/formul%C3%A1rio-n%C3%A3o-importa-values-do-dados

  • your form is somewhat curious, disabled with required but cannot edit

  • Because I want this field to exist and be filled in with information that comes from the bank table, but cannot be edited. This is only to show the user the information that he typed in the previous registration, but that he cannot change. The only changeable field is the last, the Realized.

Show 9 more comments

1 answer

3


The <input type="hidden" name="id" value="<?php echo $id ?>" /> by being inside <fieldset disabled> not being able to edit does not pass the value.

Since the id input is of type Hidden you can put it at the end of the form, outside the <fieldset disabled> altering the name="id" for name="ID" because it seems to be also problem Case-sensitive

 ................
 ................
 <input type="hidden" name="ID" value="<?php echo $id ?>" />
 </form>

or if you prefer not to change the input name

in the issue $id = filter_input(INPUT_POST, 'id');

in the update $id = filter_input(INPUT_POST, 'id');

for this id in the filter_input seems to be Case-sensitive

Browser other questions tagged

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