Personal am making a form in php ,however he does not want to add the data in the database

Asked

Viewed 45 times

-2

I am making a form in php ,but he does not want to add the data in the database, can you see what is wrong? is not generating any error.

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="utf-8">
        <title>CRUD - Cadastrar</title>     
*********************************************************************************************
<?php
session_start();
include_once("conexao.php");

$nome = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_STRING);
$telefone = filter_input(INPUT_POST, 'telefone', FILTER_SANITIZE_STRING);
$cpf = filter_input(INPUT_POST, 'cpf', FILTER_SANITIZE_STRING);
$endereco=$_POST["endereco"];//atribuição do campo"endereco"vindo do formulário para variavel

$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);





$result_usuario = "INSERT INTO cadastro2(nome,telefone,cpf,endereço, email, created) VALUES ('$nome','$telefone','$endereco,'$cpf','$email', NOW())";
$resultado_usuario = mysqli_query($conn, $result_usuario);

if(mysqli_insert_id($conn)){
    $_SESSION['msg'] = "<p style='color:green;'>Usuário cadastrado com sucesso</p>";
    header("Location: index.php");
}else{
    $_SESSION['msg'] = "<p style='color:red;'>Usuário não foi cadastrado com sucesso</p>";
    header("Location: index.php");
}

*******************************************************************************************
<?php
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "cadastro";

//Criar a conexao
$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);
if (!$conn) {

}
    </head>
    <body>
        <h1>Cadastrar Usuário</h1>
        <?php
        if(isset($_SESSION['msg'])){
            echo $_SESSION['msg'];
            unset($_SESSION['msg']);
        }
        ?>
        <form method="POST" action="processa.php">
            <label>Nome: </label>
            <input type="text" name="nome" placeholder="Digite o nome completo"><br><br>

            <label>Telefone: </label>
            <input type="Telefone" name="Telefone" placeholder="Digite o seu telefone"><br><br>

            <label>CPF: </label>
            <input type="CPF" name="CPF" placeholder="Digite o seu CPF"><br><br>

            <label>Endereço: </label>
            <input type="Endereço" name="Endereço" placeholder="Digite o seu endereço"><br><br>

            <label>E-mail: </label>
            <input type="email" name="email" placeholder="Digite o seu melhor e-mail"><br><br>

            <input type="submit" value="Cadastrar">
        </form>
    </body>
</html>
  • But where’s the file processa.php? Without it there is no way to know why data is not saved.

  • $result_usuario = "INSERT INTO cadastre 2(name,phone,Cpf,address, email, created) VALUES ('$name','$phone','$address,'$Cpf','$email', NOW()"; $resultado_usuario = mysqli_query($Conn, $result_usuario); if(mysqli_insert_id($Conn)){ $_SESSION['msg'] = "<p style='color:green;'>Successfully registered user</p>"; header("Location: index.php"); }Else{ $_SESSION['msg'];}

  • Edit and ask by pressing the button edit and put this information into the body of the question.

1 answer

0

The keys in the superglobal $_POST comes from the body of the request, in your case, comes from the names of the inputs. From what I saw, the address field is named "Address" in index.php and "address" in processes.php.

This field may be set to NOT NULL, causing error inserting.

Now for PHP not showing any error, it might be some configuration in it.

  • Successfully registered user "; header("Location: index.php"); }Else{ $_SESSION['msg'] = " User was not successfully registered "; header("Location: index.php"); }

  • This error appeared now, I switched c to address in index.php

  • This error appeared to be in Else condition, see which error was returned with: mysqli_connect_error()

Browser other questions tagged

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