Problem entering form data in Mysql

Asked

Viewed 33 times

0

Good morning, I created a form for registration and this form should send via POST without leaving the page and perform the Insert in my database table, but it enters every time in my "Else{echo "Error, unable to enter in the database";}"

I really can’t see why I’m not entering in the database. Page code

<form class="user-menu-form" action="<?php echo $_SERVER["PHP_SELF"];?>" method="POST">
    <nav class="user-menu-form-nav">

        <label class="menu-form-label">Nome:</label>
        <input class="user-menu-input" type="text" placeholder="Nome" required="required" name="nome_resp" autofocus>
        <br>

        <label class="menu-form-label">CPF ou CNPJ:</label>
        <input class="user-menu-input" type="text" placeholder="CPF ou CNPJ" required="required" name="cpfcnpj_resp">
        <br>
        <label class="menu-form-label">CEP:</label>
        <input class="user-menu-input" type="text" placeholder="CEP" required="required" id="cep" name="cep_resp" max-length="8">
        <br>
        <label class="menu-form-label">Endereço:</label>
        <input class="user-menu-input" type="text" placeholder="Endereço" id="rua" required="required" name="endereco_resp">
        <br>

        <label class="menu-form-label">Complemento:</label>
        <input class="user-menu-input" type="text" placeholder="complemento" id="complemento" required="required" name="complemento_resp">
        <br>
        <label class="menu-form-label">Bairro:</label>
        <input class="user-menu-input" type="text" placeholder="Bairro" id="bairro" required="required" name="bairro_resp">
        <br>
        <label class="menu-form-label">Cidade:</label>
        <input class="user-menu-input" type="text" placeholder="Cidade" required="required" id="cidade" name="cidade_resp">
        <br>
        <label  class="menu-form-label">UF:</label>
        <input class="user-menu-input" type="text" placeholder="UF" required="required" name="uf_resp" id="uf">
        <br>
        <label class="menu-form-label">País:</label>
        <input class="user-menu-input" type="text" placeholder="País" required="required" name="pais_resp">
        <br>
        <label class="menu-form-label">E-mail:</label>
        <input class="user-menu-input" type="email" placeholder="Email" required="required" name="email_resp">
        <br>
        <label class="menu-form-label">Telefone:</label>
        <input class="user-menu-input" type="text" placeholder="Telefone" required="required" name="fone_resp">
        <br>
        <label class="menu-form-label">Celular:</label>
        <input class="user-menu-input" type="text" placeholder="Celular" required="required" name="cel_resp">
        <br><label class="menu-form-label">Tutelado:</label>
        <input class="user-menu-input" type="text" placeholder="Responsável" required="required" name="responsavel_usuarios1">
        <br>
        <br><label class="menu-form-label">Tutelado:</label>
        <input class="user-menu-input" type="text" placeholder="Responsável" name="responsavel_usuarios2">
        <br>
        <br><label class="menu-form-label">Tutelado:</label>
        <input class="user-menu-input" type="text" placeholder="Responsável" name="responsavel_usuarios3">
        <br>
    </nav>
    <br>
    <br>
    <button class="menu-form-btn" type="submit" method="POST"><img src="assets/imgs/check.png" width="12px;">Cadastrar</button>
    <button class="menu-form-btn"><img src="assets/imgs/clipboard.png" width="16px;"><a href="listagem.php">Listagem</a></button>
    <?php
    include 'conecta.php';
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $cpfcnpj_resp        = $_POST['cpfcnpj_resp'];
        $nome_resp           = $_POST['nome_resp'];
        $endereco_resp       = $_POST['endereco_resp'];
        $complemento_resp    = $_POST['complemento_resp'];
        $bairro_resp         = $_POST['bairro_resp'];
        $cep_resp            = $_POST['cep_resp'];
        $cidade_resp         = $_POST['cidade_resp'];
        $uf_resp             = $_POST['uf_resp'];
        $pais_resp           = $_POST['pais_resp'];
        $email_resp          = $_POST['email_resp'];
        $fone_resp           = $_POST['fone_resp'];
        $cel_resp            = $_POST['cel_resp'];
        $responsavel_usuarios1   = $_POST['responsavel_usuarios1'];
        $responsavel_usuarios2   = $_POST['responsavel_usuarios2'];
        $responsavel_usuarios3   = $_POST['responsavel_usuarios3'];

        $sql  = "INSERT INTO responsavel (cpf_cnpj_resp,nome_resp,endereco_resp,complemento_resp,bairro_resp,cep_resp,cidade_resp,uf_resp,pais_resp,email_resp,fone_resp,cel_resp,hospede_1_resp_cpf,hospede_2_resp_cpf,hospede_3_resp_cpf) VALUES ('$cpfcnpj_resp','$nome_resp','$endereco_resp','$complemento_resp','$bairro_resp','$cep_resp','$cidade_resp','$uf_resp','$pais_resp','$email_resp','$fone_resp','$cel_resp','$responsavel_usuarios1','$responsavel_usuarios2','$responsavel_usuarios3')";
        mysqli_query($conecta,$sql); //Realiza a consulta
        if(mysqli_affected_rows($conecta) == 1){
            echo "<h4> gravado com sucesso</h4>";
            sleep(2);
        } else {
            echo "Erro, não possível inserir no banco de dados";
        }
        mysqli_close($conecta); //fecha conexão com banco de dados
    }
    ?>
</form>

connects.php

<?php
$conecta = mysqli_connect('localhost', 'root', '') or trigger_error(mysql_error());
// Tenta se conectar a um banco de dados MySQL
mysqli_select_db($conecta, 'car0000001') or trigger_error(mysqli_error());
mysqli_set_charset($conecta, 'utf8');
?>
  • $connects = mysqli_connect('localhost', 'root', ') or trigger_error(mysql_error()); --> Here " i " is missing in mysql_error(). And in your if to check if it was successfully changed, I think it would be better to check if it is equal to one, check if it is != 0 . Maybe it’ll help

  • Vitor, I did what you said, it came out of my IF and entered the IF but NOTHING was registered in the database...

1 answer

1


Check that the value of the sql variable is correct, debugge and run the command directly in the database and see if it is being inserted.

Browser other questions tagged

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