Non-functional PHP Mysql database

Asked

Viewed 73 times

1

I have a PHP form with a Mysql database generated by phpMyAdmin, but the database 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>

Below is the connection, on another page, connects.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>

Besides not saving, nothing appears on the screen, not even the JS Alerts I made. Follow 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 appears that it was saved, but when I check the bank has no record. inserir a descrição da imagem aqui **If anyone can tell me the problem and how to solve it, I thank you.

2 answers

3


It happens when you omit the value of action processing takes place on the same page.

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

When you use $nome = $_POST['nome']; that value contained within the $_POST refers to the name field, then your 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 placing the name with the respective value being received by $_POST['name_do_campo]`.

Regarding the action omitted in your form, there are some ways to process on the same page, being:

Leave blank: "action=""...

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

Another flaw lies 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’ll assume for the columns the same names of the variables, but you should check, would be like this:

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

  • Opa, now the form is receiving the right information, I can see it through a var_dump. Now the only problem is the bank that is not registering.

  • I supplemented the answer. Good Luck, if any of the answers is correct consider validating it by clicking on the green icon below the answer evaluation arrows.

  • I made the suggested changes besides passing the connection to a "connect.php" page where I put a H1 written saved successfully. But it gives the following error that I will edit.

  • Just look at the image is difficult, what I can see is that the code is duplicated in your editor, try to post the code here exactly like the one in your editor...

  • I don’t think it’s necessary... You can edit this one yourself...

  • Ready-made, edited. :)

Show 2 more comments

1

You didn’t put the action in his form.

That:

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

That’s got to be it:

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

Taking advantage of: Your buttonIt’s got to be the kind submit

                         <div class="">
                        <button type="submit" class="btn btn-primary btn-lg btn-block">Salvar</button>
                    </div>
  • In this action you have to have the file of this form page?

  • the action points to the page that has the php code that makes the insertion in the bd

  • Then I would have to separate the link from the form page?

  • Opa, now the form is getting the values right. I can see this through a var_Dump.

  • Put a $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); to check if there is an error in your PHP. Although you are using a PHP form earlier than the version of this syntax....

  • So, I tried to separate the php part of the same form page to, at the time of the action, redirect to another page and not to the same. The word is that there is an error in apache to load the page.

  • after it is saved, place a header to redirect to another page. Since the saved page has no html code.

  • So, I passed the connection to a separate file connects.php and put in it an H1 with any text. But it gives the following error that I will edit.

Show 3 more comments

Browser other questions tagged

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