Form data is not being registered

Asked

Viewed 978 times

0

I have a form written in php with connection to a database administered by phpMyAdmin.

WHAT SHOULD HAPPEN:

The data entered in the form should be registered showing a alert of "successfully saved" and back to the previous page.

WHAT’S GOING ON: When trying to register, even redirects to the page responsible for registering and show the alert, but the page is blank, no one appears alert, does not return to the previous page or register the information in the bank.

FORM:

<div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h1 style="
                    margin-top:100px;">Cadastro de Formações</h1>
                <p> </p>
                <p class="lead"></p>
                <ul class="list-unstyled">
                    <form id="cadastro" name="cadastro" method="post" action="banco/updateF.php" style="
                        text-align: left;
                        margin-top:50px;">
                        <div class="col-lg-12">
                            <div class="form-group" style="
                        text-align: left;">
                                <label  for="NOME">Nome: </label>
                                <input  type="text" required class="form-control" id="NOME" name="NOME" placeholder="Nome da formação">
                             </div>
                        </div>

                        <div class="col-lg-12">
                            <div class="form-group" method="post" style="
                        text-align: left;">
                                <label  for="CARGA">Carga Horária: </label>
                                <input  type="text" required class="form-control" id="CARGA" name="CARGA" placeholder="Carga horária da formação">
                             </div>
                        </div>
                        <div class="col-lg-12">
                            <div class="form-group" method="post" style="
                        text-align: left;">
                                <label  for="OBJETIVO">Objetivo: </label>
                                <input  type="text" required class="form-control" id="OBJETIVO" name="OBJETIVO" placeholder="Objetivo da formação">
                             </div>
                             <div class="form-group" method="post" style="
                        text-align: left;">
                                <label for="CONTEUDO">Conteúdo da programático: </label>
                                <textarea class="form-control" id="CONTEUDO" rows="3" name="CONTEUDO" placeholder="Conteúdo programático da formação"></textarea>
                             </div>
                             <div class="">
                                <button type="submit" class="btn btn-primary btn-lg btn-block">Salvar</button>
                            </div>
                            <div class="alert alert-info" role="alert">
                                <strong>Hey! </strong> Antes de realizar o cadastro, certifique-se de que não se esqueceu de nada! :)
                            </div>
                        </div>
                     </form>
                </ul>
            </div>
        </div> 
    </div>

THE CONNECTION:

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

THE UPDATE:

    <!-- Envia dados do formulario de edicao pro banco de dados -->

<!--TESTE DE BANCO -->

<?php 
require ("conecta.php");
//coletando dados do formulario

    $nome           =   $_POST["NOME"];
    $carga          =   $_POST["CARGA"];
    $objetivo       =   $_POST["OBJETIVO"];
    $conteudo       =   $_POST["CONTEUDO"];

// Inserir dados no banco
    $itens = $_POST['NOME'];

    if (!empty($itens)){
        $itens = implode(',', $_POST['NOME']);
    }
$up = "UPDATE turmas SET NOME = '$nome', CARGA = '$carga', OBJETIVO = '$objetivo', CONTEUDO = '$conteudo'";
echo $up;

$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>';
    }
?>

Quando eu tento mostrar o código fonte pelo navegador só aparecem os comentários, mas a página da conexão não.

When I try to display the source code of this blank page by the browser only the comments of the update page appear.

I’m not very experienced, but from what I’ve studied, it doesn’t seem wrong. If you could point out the mistakes, I’d really appreciate it. :)

  • 2

    What version of php?

  • This is something I forgot to mention. My php version is old and as there are many systems running in this version, I can’t update.

  • 4

    Mariana, please don’t keep adding HELP in the title, just the fact you asked on the site we already understand that you need help with your problem.

  • Ta, I was going to take off after a few minutes.

  • But you cannot do this simply in order to "raise" the question. If the answers have not solved the problem, comment below them. Or edit the question by providing more details. You can also read [Ask] to learn how to improve the question.

  • Yes, but I’m already providing all the details, I’ve commented below the answers, I’m studying, trying to solve but I can’t find the problem and it’s been a few days. I know no one has an obligation to answer me, it was just an attempt.

Show 1 more comment

3 answers

2

I may be wrong, but from what I saw in the data fill form, you don’t have the entry for the ID, and in PHP code both in data recovery and in the update query you use an id that doesn’t exist. Do you understand what I mean? Look at the code again and see in the form that you don’t have a field to insert the id, and in your php code it looks like this:

   $id             =   $_POST["ID"]; //essa linha aqui não está recuperando nada
   $nome           =   $_POST["NOME"];
   $objetivo       =   $_POST["OBJETIVO"];
   $conteudo       =   $_POST["CONTEUDO"];
   $carga          =   $_POST["CARGA"];

Right away we have:

    $up = "UPDATE formacoes SET NOME = '$nome', OBJETIVO = '$objetivo', CONTEUDO = '$conteudo', CARGA = '$carga'  WHERE id = '$id'";
//nessa linha você está usando um id inexistente

Maybe that’s why the update is not being carried out. Check there and give feedback!

  • Very well noticed Cloudac +1. Where the id field to be posted Mariana should be a <input type='Hidden' name='id' value='5'>

  • Wow, that makes a lot of sense! I had put the ID because there is a field in the table that is an auto increment ID so I was wondering if I should put or not.

  • Yes I passed the eye and I didn’t notice, I fixed it and see if it works and take advantage of the code I mounted below and improve yours that is very messy writing.

  • Okay, I made the recommended changes. Let me ask you a question: My background has an ID. When I sign up a class, I’m gonna need the ID of the training to say that the class belongs to that formation, right? Other formations will be registered, so I wouldn’t need Where to tell which formation the data should be updated?

  • Mari better ask another question or it takes the cohesion of this post. And then you know the moderators come up! = P

  • Ah, right, I’ll assemble and put the link here, okay? :)

Show 1 more comment

1

Mariana, if you are sending javascript to the client’s browser it is necessary to send the correct header, in case before your echo do this :

header('Content-Type: application/javascript');
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>';
}    

In the above case I am sending to the browser and warning that the return code of php is a javascript, so that it interprets the routine.

Updating

  <?php 
       require ("conecta.php");
       //coletando dados do formulario
       $id             =   $_POST["ID"]; 
       $nome           =   $_POST["NOME"];
       $objetivo       =   $_POST["OBJETIVO"];
       $conteudo       =   $_POST["CONTEUDO"];
       $carga          =   $_POST["CARGA"];

       // Inserir dados no banco
       $itens = $_POST['NOME'];

       if (!empty($itens)){
           $itens = implode(',', $_POST['NOME']);
       }
       $up = "UPDATE formacoes SET NOME = '".$nome."', OBJETIVO = '".$objetivo."', 
       CONTEUDO = '".$conteudo."', CARGA = '".$carga."'  WHERE id =".$id;
       $up= mysql_query($up) or die(mysql_error());

       echo "Número de linhas afetadas:".mysql_affected_rows()."<br/>";
       if (mysql_affected_rows() > 0) { 
            $resposta = 'Update concluído com sucesso!';    
       } else{    
           $resposta = 'Cadastro não sofreu alterações!';   
       }
       mysql_close($conexao);
       die($resposta);
   ?>

Run that script and tell me what you got?

  • So I did, but it’s still the same thing. The funny thing is that this same update works on another system.

  • Do a test as soon as the bank page appears with the right click on it to display the source code and check if you have the contents of your echo.

  • So you’re not showing the content, which leads me to believe that maybe the problem is in the form. :/

  • Does this php database/updateF.php page exist? You’re posting to it, is that the name ? If the form is online send the address so I can check.

  • So, if it is an internal system then it is not online. But this page updateF.php does exist, the page is redirected to it. I’ll put a picture of what’s going on.

  • So I ran with this script and gave the same thing, nothing new happened. :/

  • Yeah, nothing yet. @Rafaelsalomão

Show 2 more comments

0


Nothing was wrong with the form, the error was in the update file. I reworked it and solved it like this:

<!--ENVIA OS DADOS DO FORMULÁRIO PARA O BANCO-->

<?php

    $nome = $_POST['NOME'];
    $carga = $_POST['CARGA'];
    $objetivo = $_POST['OBJETIVO'];
    $conteudo = $_POST['CONTEUDO'];

    $strcon = mysqli_connect('localhost','root','', 'db_formacao') or die('Erro ao conectar ao banco de dados');
    $sql = "INSERT INTO formacoes VALUES ('$id', '$nome', '$carga', '$objetivo', '$conteudo')"; 
    mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro");
    mysqli_close($strcon);

    echo "Formação cadastrada com sucesso!";
?>

I thank everyone who helped me.

Browser other questions tagged

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