Update does not work

1

In my system there is a registration page and a page with a table of the information of the registered ones. This table has an edit button that links to an equal form on the registration page.

What should happen:

When you click the save button, the modified fields should be changed in the registry.

What's happening:

When I try to change the registry, the alert of "saved successfully" appears and returns to the previous page, as it should. But by looking through 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();
    }
?>

Edit 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> 

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

    
asked by anonymous 05.09.2017 / 15:19

3 answers

2

See if this update works! If you look closely, there are some conflicts with double and single quotation marks, as well as comma-separated errors.

To edit only the column with the id you need, the $ id you will also have to get as POST:

<?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: </label>
                                <input  type="number" required class="form-control" id="ID" 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 class="form-check">
                            <label class="form-check-label">
                                <input class="form-check-input" type="radio" name="REALIZADO" id="REALIZADO" value="REALIZADO" aria-label="...">REALIZADO
                            </label>
                        </div>
                        <div class="">
                            <button type="submit" class="btn btn-primary btn-lg btn-block">Salvar</button>
                        </div>
                    </div>
                </form>
            </ul>
        </div>
    </div>
</div> 

    $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_close($strcon);

    echo '<script type="text/javascript">
                alert("Salvo com Sucesso !");
                window.history.go(-1);
            </script>';
    ?>
    
05.09.2017 / 15:56
2

Have you tried to give a var_dump in Query that is running and run it directly in PhpMyAdmin ? Generally, MySql gives more relevant information about Query error ...

Another tip is to try to create Query directly in MySql , (in your case, in the PhpMyAdmin interface) , and once it works, migrate it to PHP .

    
05.09.2017 / 16:00
1

In your query, UPDATE participantes SET REALIZADO='$realizado' WHERE id='$id' , the WHERE clause tries to use the variable $ id, which was not declared before, and is also not being passed by the form. You need to create the ID field in the HTML form and pass it.

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

Other points to note:

Error Message

This message does not help much, it would be interesting to also log the error, or even write the error directly in the message, however the latter case may not be interesting for showing the problem to the end user.

If you have no problem showing, you can change the code to:

mysqli_query($strcon,$sql) or die("Erro ao tentar atualizar registro. " . mysqli_error($strcon));

Use prepared statement

As it is, your query is not secure, take a look at this subject. Learn more here: link

Writing in Attributes

In the current code, if the employee's name is written as John "Mito" da Silva , he will break his attributes. Uses where you write the attributes the following:

<label  for="COLABORADOR">Colaborador: </label>
<input  type="text" required class="form-control" id="COLABORADOR" name="COLABORADOR" value="<?php echo htmlentities($colaborador, ENT_QUOTES); ?>" />

Note also that I have changed the for attribute of label since it is the label of this input.

    
05.09.2017 / 17:25