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. **If anyone can tell me the problem and how to solve it, I thank you.
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.
– Mariana Bayonetta
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.
– MagicHat
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.
– Mariana Bayonetta
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...
– MagicHat
I don’t think it’s necessary... You can edit this one yourself...
– MagicHat
Ready-made, edited. :)
– Mariana Bayonetta
Let’s go continue this discussion in chat.
– MagicHat