Form does not receive registered data

Asked

Viewed 219 times

0

What’s going on:

I have a system that, after completing the form and saving, does not save the changes by appearing a alert "saved without changes" and back to previous page.

It does not seem to be a connection problem with the bank or the bank itself, but the form that does not recognize when some field has been changed.

Follow part of the form:

    <?php
    // Busca valores no banco de dados para preenchimento do formulário que quer alterar.
    $id = $_GET['id'];
    $sql = (" SELECT * FROM r WHERE id = '{$id}'");
    $query=mysql_query($sql);
    while ($campo = mysql_fetch_assoc($query)) {  
    ?>

    <div class="container">
        <form method="post" action="../std/update.php" enctype="multipart/form-data" name="r" class="formulario">
            <div class="container container-divisor">
                <h4>Descrição R</h4>
                <div class="row">
                    <input type="hidden" name="idnovo" id="idnovo" value="<?=$campo['id'];?>">
                    <input type="hidden" name="dataedicao" id="dataedicao" value="<?php echo date('d/m/Y'); ?>">
                    <div class="col-md-4">
                        <label for="datacriacao">Data</label>
                        <input type="text" class="form-control" name="datacriacao" id="dataCriacao" value="<?=$campo['datacriacao'];?>"127>
                    </div>
                    <div class="col-md-4">
                    </div>
                </div>
            </div>
        </form>
    </div>

Connection to the bank:

<?php
    mysql_connect("localhost","root","") or die ("erro na conexao com o banco de dados!");
    mysql_select_db("db_r");
?>

Bank update:

<?php 
include ("conecta.php");

    $id             =   $_POST["idnovo"]; 
    $dataedicao     =   $_POST["dataedicao"];
    $datacriacao    =   $_POST["datacriacao"];
    $statusgeral    =   $_POST["statusgeral"];

// Inserir dados no banco
    $itens = $_POST['m'];
//$_POST['m'] as $itens
    if (!empty($itens)){
 $itens = implode(',', $_POST['m']);
}

   $up= ("UPDATE r SET dataedicao = '$dataedicao', datacriacao = '$datacriacao', statusgeral = '$statusgeral', criadopor = '$criadopor', origem = '$origem', projet = '$projet', jalon = 'jalon', domaine = '$domaine', classification = '$classification', link = '$link', themes = '$themes', description = '$description',conseil = '$conseil', recidiv = '$recidiv', priorite = '$priorite', pilote = '$pilote', itens = '$itens', refstandard = '$refstandard', titre = '$titre', modif = '$modif', respect = '$respect', pourquoi = '$pourquoi', dataplan1 = '$dataplan1', action1 = '$action1', pilote1 = '$pilote1', metierp1 = '$metierp1', dateprevu1 = '$dateprevu1', datereal1 = '$datereal1', status1 = '$status1', link1 = '$link1', type1 = '$type1', datacomm1 = '$datacomm1', commentaire1 = '$commentaire1', suivre = '$suivre', dataplan2 = '$dataplan2', action2 = '$action2', altis2 = '$altis2', pilote2 = '$pilote2', metierp2 = '$metierp2', dateprevu2 = '$dateprevu2', datereal2 = '$datereal2', status2 = '$status2', link2 = '$link2', type2 = '$type2', datacomm2 = '$datacomm2', commentaire2 = '$commentaire2' WHERE id = '$id' ;");

$up= mysql_query($up) or die(mysql_error());




?>
    <?php
    if (mysql_affected_rows() > 0) { 
        echo '<script type="text/javascript">
            alert("Salvo com Sucesso !");
            window.history.go(-1);
        </script>';    
    } else {    
        echo '<script type="text/javascript">
            alert("Salvo sem Modificações !");
            window.history.go(-1);
        </script>';
    }
?>
<?php           
    mysql_close($conexao);
?>
</body>
</html>

The Alert: inserir a descrição da imagem aqui The information changed in the form is not being saved in the bank, I searched a lot for an error but so far nothing. If you could point out the mistake or suggest something to me, I’d appreciate it.

UPDATING:

The problem has not yet been solved, but I gave a echo in $up and I realized that the form is not with any problem, since they are printed on the screen all the changed information. But still this information is not being saved in the bank, so the problem must be in Update. Another thing that is being printed is the error: "Unknown column in field list"

  • I updated the answer. I hope it helps.

  • 1

    You’re using functions like mysql_ suggest reading this: Why we should not use mysql type functions_

  • In PHP version 5.5 these functions are already discontinued, and in PHP 7 they don’t even work anymore.

  • @Knautiluz I switched everything to mysqli_, but then I re-did it with mysql_ because I think her system is completely on mysql_, so it would be a lot of work. mysql_ still works well in Php 5.6

  • My php version is very old and many systems here use this version. I’m not allowed to upgrade, so I can’t use mysqli. :/

1 answer

2


Hello. I noticed two things there in your code.

The first is the line: //$up = mysql_query("UPDATE retex SET dataedicao(...)

Is this line really commented on? Try removing the "/", as this indicates a comment, so the interpreter ignores the instruction, and it is just the instruction to save the changes in the database (it is not?).

Second thing I noticed is in this excerpt:

<?php
    if(mysql_affected_rows() > 0){
    ?>  
<script type="text/javascript">
    alert("Salvo com Sucesso !");
    window.history.go(-1);
</script>
<?php
        }
        else{
?>
</script>
<script type="text/javascript">
    alert("Salvo sem Modificações !");
    window.history.go(-1);
</script>

Note that the tags script are outside the Php blocks, so they will be printable (echo) anyway. Thus, both the first and the second block of scripts will be present in the final file that will be sent to the browser. So try switching to:

<?php
    if (mysql_affected_rows() > 0) { 
        echo '<script type="text/javascript">
            alert("Salvo com Sucesso !");
            window.history.go(-1);
        </script>';    
    } else {    
        echo '<script type="text/javascript">
            alert("Salvo sem Modificações !");
            window.history.go(-1);
        </script>';
    }
?>

Other detail: close tags input with /> instead of just >. I know the most modern browers ignore, but it is interesting to test to see.

UPDATE: I noticed two more things.

First: the variable $statusgeral is not filled in, right? It seems that the $_POST['statusgeral'] does not exist, since there is no such field in the form.

Second thing: the closure of input text is 127>, change to only >. That might be bugging the form.

UPDATE 2

I replicated the application here and it worked perfectly with small changes I made. The code is commented but, having any doubt, just speak.

connects.php: unchanged here

<?php
mysql_connect("localhost","root","") or die ("erro na conexao com o banco de dados!");
mysql_select_db("db_r");

Form page:

<?php
    // Busca valores no banco de dados para preenchimento do formulário que quer alterar.
    // Mudei include para require
    require 'conecta.php';
    $id = $_GET['id'];
    $query = "SELECT * FROM r WHERE id = '{$id}'";
    $query = mysql_query($query) or die (mysql_error());   
 ?>

<div class="container">
<!-- Iniciei o "while loop" aqui embaixo.
OBS: corrija o URL da action="" para o seu URL. Eu mudei pois fiz tudo em uma só pasta -->
<?php while ($campo = mysql_fetch_assoc($query)) : ?>
<form method="post" action="./update.php" enctype="multipart/form-data" name="r" class="formulario">
    <div class="container container-divisor">
        <h4>Descrição R</h4>
        <div class="row">
            <input type="hidden" name="idnovo" id="idnovo" value="<?=$campo['id'];?>">
             <!--  Alterei o value do dataedicao para o formate Ano/Mes/Dia Hora:minutos:segundos (timestamp). -->
            <input type="hidden" name="dataedicao" id="dataedicao" value="<?= date("Y/m/d H:i:s") ?>">
            <div class="col-md-4">
                <label for="datacriacao">Data</label>
                <input type="text" class="form-control" name="datacriacao" id="dataCriacao" value="<?=$campo['datacriacao'];?>"127>
                <!-- inseri um botão para enviar o formulário, uma vez que não sei como você está fazendo para enviá-lo -->
                    <input type="submit" value="enviar" id="send" />
            </div>
            <div class="col-md-4">
            </div>
        </div>
    </div>
</form>
<?php endwhile; ?>
</div>

Update.php

<?php 
// Mudei o include para require
require 'conecta.php';

// tudo igual por aqui, apenas atribui um valor qualquer à variável $statusgeral
$id             =   $_POST["idnovo"]; 
$dataedicao     =   $_POST["dataedicao"];
$datacriacao    =   $_POST["datacriacao"];
$statusgeral    =   'ativo';

// Montei o texto da query fora do mysql_query e alterei tudo para o formato '{$campo}', ao invés de só '$campo'    
$qr = "UPDATE retex SET dataedicao = '{$dataedicao}', datacriacao = '{$datacriacao}', statusgeral = '{$statusgeral}' WHERE id = '{$id}' ";
$qr = mysql_query($qr) or die(mysql_error());

// Verifica se foram atualizados os dados e printa na tela 
if (mysql_affected_rows() > 0):
    echo "Atualizado";
else:
    echo "Nada foi atualizado";
endif;

NOTE: as the id is being passed via GET, it is important to remember that you need to inform it in the URL. It occurred to me that you might be forgetting this. Stay like this: http://localhost/paginadeatualize/? id=numeroDoId

Update 3

$up= "UPDATE r SET dataedicao = '$dataedicao', datacriacao = '$datacriacao', statusgeral = '$statusgeral', criadopor = '$criadopor', origem = '$origem', projet = '$projet', jalon = 'jalon', domaine = '$domaine', classification = '$classification', link = '$link', themes = '$themes', description = '$description',conseil = '$conseil', recidiv = '$recidiv', priorite = '$priorite', pilote = '$pilote', itens = '$itens', refstandard = '$refstandard', titre = '$titre', modif = '$modif', respect = '$respect', pourquoi = '$pourquoi', dataplan1 = '$dataplan1', action1 = '$action1', pilote1 = '$pilote1', metierp1 = '$metierp1', dateprevu1 = '$dateprevu1', datereal1 = '$datereal1', status1 = '$status1', link1 = '$link1', type1 = '$type1', datacomm1 = '$datacomm1', commentaire1 = '$commentaire1', suivre = '$suivre', dataplan2 = '$dataplan2', action2 = '$action2', altis2 = '$altis2', pilote2 = '$pilote2', metierp2 = '$metierp2', dateprevu2 = '$dateprevu2', datereal2 = '$datereal2', status2 = '$status2', link2 = '$link2', type2 = '$type2', datacomm2 = '$datacomm2', commentaire2 = '$commentaire2' WHERE id = '$id'";
  • Thank you for answering this question! The commented line is only commented on the question, it was an error when pasting the code, I will edit. The issue of scripts was even a question I had, I will do as suggested and return. :)

  • So I edited with the result.

  • Mariana, how are you sending the form? I have not seen any send button (type Submit) or even button. Maybe you’re not recording because you’re not receiving the data.

  • Also take the last comma before the "WHERE" in your query.

  • So, the form is being sent via a normal Submit button.

  • I removed it, but still the same. :/

  • 1

    Okay. I don’t understand it either. I’ll take a closer look at this.

  • I updated the answer. If not, I will replicate the application here and see the result later, because now I have no time. I need to leave.

  • The status variable exists. It’s just that this form is actually quite large, but by confidentiality rules I couldn’t put all the fields in that question. But nothing relevant, is the same thing of those three camps placed in the question. Alright, already helped a lot, I await your answer. :)

  • I made the suggested changes but the form no longer works. :/

  • I tested the change by changing the form page. Since the first one it does not work.

  • As for the changes made on the update page, when saving is printed on the screen: Unknown 'Conseil' column in 'field list', there really exists in the table a column called Conseil

  • @Marianaferreira But did you change the table name? Note that I did everything in one table, the table "r". To get back to normal, you need to switch to "Retex" again. (that’s what’s in the question). I’ll edit the answer.

  • Yeah, I did that.

  • 1

    Your problem is complicated. I tested it here in version 5.6 of Php, with Mysql database and it was all normal. I did not put the "Trust" column in the bank because it is not in the question. I recommend that when you ask a question, put the full code. No need to put confidential data, you can replace them. As in software production there is a lot of code coupling, posting only one part can give a wrong view of the problem. According to what you described, I did the same application and it worked, but the problem may not be there where you think, Mariana.

  • Okay, I edited the question with the whole query. Only in some variables it gives this error and I do not understand why, since all are equal.

  • I updated it. I just changed $up, because I had a syntax error there. Test there and let’s see.

  • Thank you for the correction! Unfortunately you’re making the same mistake. I’m sorry to be so insistent, is that my deadline to solve this problem ends today. :/

  • If you want to help here: https://answall.com/questions/223600/banco-data

  • https://answall.com/questions/226529/dados-de-formul%C3%A1rio-n%C3%A3o-est%C3%A3o-being-registered/226547? noredirect=1#comment463963_226547

Show 16 more comments

Browser other questions tagged

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