PHP MySQL non-functional database

1

I have a PHP form with a MySQL database generated by phpMyAdmin, however, the bank does not receive the information entered in the form.

Follow the form code:

<!-- content -->
    <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" 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" class="form-control" id="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" class="form-control" id="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" class="form-control" id="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" placeholder="Conteúdo programático da formação"></textarea>
                             </div>
                             <div class="">
                                <button type="button" class="btn btn-primary btn-lg btn-block">Salvar</button>
                            </div>
                        </div>
                     </form>
                </ul>
            </div>
        </div> 
    </div>

Follow the link below, on another page, connect.php:

  <?php
    include("conecta.php");
    $conn = new mysqli ("localhost", "root", "", "db_formacoes");

    $nome = $_POST['nome'];
    $objetivo = $_POST['objetivo'];
    $conteudo = $_POST['conteudo'];
    $carga = $_POST['carga'];

     $query = "INSERT INTO formacoes (nome,objetivo,conteudo,carga)  VALUES('$nome','$objetivo','$conteudo', '$carga')";

    $resultado = mysqli_query($conn,$squery);

    if(!mysqli_query($conn, $squery)){  
        echo 'Opa, não conseguimos nos conectar ao banco de dados. '. mysqli_error($conn);
    }else{
        echo 'Operação realizada com sucesso';
    }

    mysqli_close($conn);
?>
<script type="text/javascript">
    alert("Salvo com Sucesso !");
    window.history.go(-1);
</script>

In addition to not saving, nothing appears on the screen, not even the alerts on JS I did. Here are the alerts:

<script type="text/javascript">
    function validaCampo()
    {
    if(document.cadastro.nome.value=="")
        {
        alert("O Campo nome é obrigatório!");
        return false;
        }
    }
    else 
        if(document.cadastro.carga.value=="");
        {
            alert("O campo carga horária é obrigatório!");
            return false;
        }
    else 
        if(document.cadastro.objetivo.value=="");
        {
            alert("O campo objetivo é obrigatório");
            return false;
        }
    else
        if(document.cadastro.conteudo.value=="");
        {
            alert("O campo conteúdo da formação é obrigatório!");
            return false;
        }
    else
        return true;
    </script> 

When I click to save it appears that it was saved, but when I check the database it does not have any records. ** If anyone can tell me the problem and how to solve it, I would appreciate it.

    
asked by anonymous 24.07.2017 / 14:19

2 answers

3

When you omit the value of action , processing occurs on the same page.

In your case, the problem is the name attributes of the fields.

When you use $nome = $_POST['nome']; this value contained within $_POST refers to the name of the field, so its input must have an attribute called name with the appropriate value ... For example:

<input type="text" class="form-control" id="NOME" name="nome" placeholder="Nome da formação">

Change your inputs by setting name to its value being received by $_POST['name_do_campo ] '.

Regarding the action omitted in your form, there are some forms for processing on the same page, being:

Leave blank: "action="" ...

Or you can use action="<?PHP echo $_SERVER['PHP_SELF']; ?>" ...

Another fault is in your query ...

$query = "INSERT INTO formacoes VALUES('$nome','$objetivo','$conteudo', '$carga' )";

You need to inform the columns and the respective values in your query , I will assume the same names for the columns, but you should check it, it would look like this:

$query = "INSERT INTO formacoes (nome,objetivo,conteudo,carga) VALUES('$nome','$objetivo','$conteudo', '$carga' )";

    
24.07.2017 / 14:58
1

You have not set action to your form .

That:

 <form id="cadastro" name="cadastro" method="post" style="
                text-align: left;
                margin-top:50px;">

It has to be this:

 <form id="cadastro" name="cadastro" method="post" action="minhapage.php" style="
                text-align: left;
                margin-top:50px;">

Taking advantage of: Your button Must be of type submit

                         <div class="">
                        <button type="submit" class="btn btn-primary btn-lg btn-block">Salvar</button>
                    </div>
    
24.07.2017 / 14:24