Form does not import "values" from database

Asked

Viewed 176 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:

The fields on this edit page should be filled with the table row information (record) that was the edit button.

When clicking the save button, the modified fields should be changed in the record.

What’s going on:

Although everything seems normal with the PHP function responsible for importing the information from this pro record value from the countryside, it’s not important.

Also, when I try to change the record Alert appears indicating it has been changed successfully, but when I open phpMyAdmin, the record has not been changed.

The connection:

<?php
    $connection = mysqli_connect("localhost", "root", "", "db_formacao");

    if (mysqli_connect_errno())
    {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

The table:

<?php
//Conexão e consulta ao Mysql
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('db_formacao') or die(mysql_error());
$qry = mysql_query("select * from participantes");

//Pegando os nomes dos campos
$num_fields = mysql_num_fields($qry);//Obtém o número de campos do resultado

for($i = 0;$i<$num_fields; $i++){//Pega o nome dos campos
    $fields[] = mysql_field_name($qry,$i);
}

//Montando o cabeçalho da tabela
$table = '<table class="table table-hover table-inverse" style="margin-top:50;background-color: #37444a; color:lightgrey;"> <tr>';

for($i = 0;$i < $num_fields; $i++){
    $table .= '<th>'.$fields[$i].'</th>';
}

//Montando o corpo da tabela
$table .= '<tbody style="
    background-color: #86979e;
    color: #37444a;    
">';
while($r = mysql_fetch_array($qry)){
    $table .= '<tr>';
    for($i = 0;$i < $num_fields; $i++){
        $table .= '<td>'.$r[$fields[$i]].'</td>';
    }

    // Adicionando botão de exclusão
    $table .= '<td><form action="FormEdicao.php" method="post">'; //formulário com método post que vai para deleteF.php
    $table .= '<input type="hidden" name="ID" value="'.$r['ID'].'">';
    $table .= '<button class="btn btn-primary">Editar</button>'; //aqui está o seu botão
    $table .= '</form></td>';
}

//Finalizando a tabela
$table .= '</tbody></table>';

//Imprimindo a tabela
echo $table;

?>

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($turma, $formacao, $colaborador);
    /* NULL NULL NULL */
?>

    <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" 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="'.['ID'].'"/>
                                </div>
                            </div>
                        </fieldset>
                        <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>
                        <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 !");

        </script>';

var_dump($previsto, $realizado, $id); 
/*Ta pegando as informações certas digitadas no campo Realizado, mas não ta armazenando*/
?>

Updating: When I save the update, the script appears that says the record was successfully changed, but when I open the table in phpMyAdmin, the record has not been changed. In addition, the form continues not importing the "values" from the bank;

Gave a var_dump($forecast, $paid, $id); and appears NULL NULL NULL

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • In the database updates normal? Or also not update, equal in values?

  • So when I try to change the message "Error while trying to change record" appears, but the connection ta certinha.

  • In the update file, try to display the error that is happening in sql. Here: try {&#xA; mysqli_query($strcon, $sql);&#xA;} catch (Exception $e) {echo mysqli_error($strcon);&#xA; die("Erro ao tentar alterar registro");&#xA;}

  • So, can not see the error because soon appears the script "saved successfully" and redirects to the previous page

  • I took out window.history.go to see if the error appeared on the update page, but no, ta blank, clean

1 answer

2


The answer was simpler than I thought.

On the page of Table we have:

$table .= '<input type="hidden" name="ID" value="'.$r['ID'].'">';

This ensures that the ID is being passed when the edit button is clicked.

Doing the same for the other fields too:

    $table .= '<input type="hidden" name="ID" value="'.$r['ID'].'">';
    $table .= '<input type="hidden" name="FORMACAO" value="'.$r['FORMACAO'].'">';
    $table .= '<input type="hidden" name="TURMA" value="'.$r['TURMA'].'">';
    $table .= '<input type="hidden" name="COLABORADOR" value="'.$r['COLABORADOR'].'">';

Problem Solved! :)

Browser other questions tagged

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