I cannot write to the PHP database

Asked

Viewed 39 times

-5

File 1 picks up the data to enter into the database, the name and email are correctly being recorded, but the phone does not go to the database.

FILE 1

<script type="text/javascript">
    function validar()
    {
        var nome = formulario.nome.value;

        // valida nome
        if (nome == "")
        {
            alert('Preencha o nome da pessoa!');
            formulario.nome.focus();
            return false;
        }

        return true;
    }
</script>




<form action="pessoas_incluir.php" method="POST" name="formulario" >

    <label>Nome da Pessoa </label>
    <input type="text" name="nome" value="" size="40" maxlength="40" required onblur="javascript:this.value=this.value.toUpperCase();" autofocus>
    <br><br>
    <label>Email</label>
    <input type="text" name="email" value="" size="40" maxlength="40" required>
    <br><br>
    <label>Telefone</label>
    <input type="text" name="fone" value="" size="15" maxlength="14" required>
    <br><br>
    <button type="submit" onClick="return validar()">Salvar</button>
    <button type="reset">Limpar</button>
</form>

PERSONAL files_include.php

<?php
 include_once "../conexao.php"; 

    $conn = mysqli_connect($localhost, $user, $password, $banco);

    if (!$conn)
    {
        echo "<script>alert('Não foi possível conectar ao Banco de Dados!');</script>";
    }
    else
    {
        // recebe parametro
        $nome = $_POST['nome'];
        $email = $_POST['email'];
        $fone = $POST['fone'];

        // monta QUERY
        $sql = "INSERT INTO PESSOAS (Nome, Email, Telefone) VALUES ('$nome','$email','$fone')";

        $result = mysqli_query($conn, $sql);

        if (! $result)
        {
            echo  "Erro na inclusão dos dados!";
        }
        else
        {
            echo  "Dados incluídos com sucesso!";
        }
    }

    mysqli_close($conn);
    header("Location: pessoas_mostra.php");
?>
  • 1

    Lucas we need more details to be able to offer you an answer.

  • what is this here? onClick="return validar()"

  • PS: Text of the question and formatted with markdown

  • It’s to validate whether something was typed or n

  • PS: I am a beginner

  • Is there an error message, strange behavior, or is there anything else you’d like to comment on? Usually who responds PHP client/server makes desktop test is a fully human job, the more information about program behaviors and logs the better to respond.

  • Does not point out any errors, simply the content of the input whose name="phone" does not write to the bank, the rest of the code is working perfectly.

  • Maybe it’s a bug

  • That’s why, on top, I asked you to put in question the code of validar().Most errors brought here occur during validation.

Show 4 more comments

1 answer

1


$fone = $POST['fone'];

on this line, replaced by

$fone = $_POST['fone'];

Missing SUPERGLOBAL UNDERLINE $_POST[]

  • puts, thank you very much, now it worked!!

Browser other questions tagged

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