PHP does not save information on Mysqli

Asked

Viewed 107 times

-1

I am making a simple registration program, but I do not know why the Form data is not being saved in the Database (MYSQL).

Can someone please help me with that??

Follows the Code:

<?php

include("conexao.php");

If(isset($_POST['nome'])){
    $nome = filter_input(INPUT_POST, "nome", FILTER_SANITIZE_STRING);
    $cpf = filter_input(INPUT_POST, "cpf", FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, "email", FILTER_SANITIZE_STRING);
    $senha = filter_input(INPUT_POST, "senha", FILTER_SANITIZE_STRING);
    $nascimento = DateTime::createFromFormat('d/m/Y',(filter_input(INPUT_POST, "nascimento", FILTER_SANITIZE_STRING)));
    $cadastro = DateTime::createFromFormat('d/m/Y',(filter_input(INPUT_POST, "cadastro", FILTER_SANITIZE_STRING)));
    $setor = filter_input(INPUT_POST, "setor", FILTER_SANITIZE_STRING);
    $cargo = filter_input(INPUT_POST, "cargo", FILTER_SANITIZE_STRING);
    $privilegio = filter_input(INPUT_POST, "privilegio", FILTER_SANITIZE_STRING);

If(!$conn){
    die("Falha na conexão com o banco de dados, procure o administrador do sistema e informe o erro abaixo: ". "<br>" .mysqli_connect_error());
}

If(empty($nome)||empty($cpf)){
    echo "Preencha os campos corretamente!";
    exit;
} else {
    $sqlusuarios = "INSERT INTO TB_USUARIOS (nome, cpf, email, senha, nascimento, cadastro, setor, cadastro, privilegio) VALUES ('".$nome."', '".$cpf."', '".$email."', '".$senha."', '".$nascimento->format('Y-m-d')."', '".$cadastro->format('Y-m-d')."', '".$setor."', '".$cargo."', '".$privilegio."')";
} If(mysqli_query($conn, $sqlusuarios)){
    echo "<script>Alert('Usuario cadastrado com Sucesso!');</script>";
} else {
    "Erro: " .$sqlusuarios. "<br>" .mysqli_connect_error($conn);
}
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>DR3 WEB</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

  <script type="text/javascript">
                function mask_date(field){
                    if(document.getElementById(field).value.length == 2){
                        document.getElementById(field).value = document.getElementById(field).value + "/";
                    }
                    if(document.getElementById(field).value.length == 5){
                        document.getElementById(field).value = document.getElementById(field).value + '/';
                    }
                }

    </script>
</head>
<body>

<br><br>
<div class="container">
    <nav class="navbar navbar-inverse">
        <div class="container-fluid">
            <div class="navbar-header">
              <a class="navbar-brand" href="#">DR3 WEB</a>
            </div>
            <ul class="nav navbar-nav">
              <li class="active"><a href="index.php">Logout</a></li>
              <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="#">Cadastros
                <span class="caret"></span></a>
                <ul class="dropdown-menu">
                  <li><a href="empresas.php">Empresas</a></li>
                  <li><a href="clientes.php">Clientes</a></li>
                  <li><a href="fornecedores.php">Fornecedores</a></li>
                  <li><a href="estados.php">Estados</a></li>
                  <li><a href="municipios.php">Municipios</a></li>
                  <li><a href="usuarios.php">Usuarios</a></li>
                  <li><a href="classificacao.php">Classificação Financeira</a></li>
                </ul>
              </li>
              <li><a href="#">Page 2</a></li>
              <li><a href="#">Page 3</a></li>
            </ul>
          </div>
</nav>
    <br>
    <h2>Cadastro de Usuarios</h2><br><hr><br>

        <form class="form-horizontal" method="post" action="usuarios.php">
          <div class="form-group">
            <label class="control-label col-sm-1" for="email">Nome:</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="nome" name="nome">
            </div>  
          </div>

          <div class="form-group">
            <label class="control-label col-sm-1" for="senha">CPF:</label>
            <div class="col-sm-4">
                <input type="text" class="form-control" id="cpf" name="cpf">
            </div>
            <label class="control-label col-sm-1" for="senha">E-mail:</label>
            <div class="col-sm-4">
                <input type="email" class="form-control" id="email" name="email">
            </div>
          </div>

          <div class="form-group">
            <label class="control-label col-sm-1" for="senha">Senha:</label>
            <div class="col-sm-3">
                <input type="password" class="form-control" id="senha" name="senha">
            </div>
            <label class="control-label col-sm-1" for="senha">Nascimento:</label>
            <div class="col-sm-2">
                <input type="datetime" class="form-control" id="nascimento" name="nascimento" onkeyup="mask_date(this.id);">
            </div>
            <label class="control-label col-sm-1" for="senha">Cadastro:</label>
            <div class="col-sm-2">
                <input type="datetime" class="form-control" id="cadastro" name="cadastro" onkeyup="mask_date(this.id);">
            </div>
          </div>

          <div class="form-group">
            <label class="control-label col-sm-1" for="senha">Setor:</label>
            <div class="col-sm-2">
                <input type="text" class="form-control" id="setor" name="setor">
            </div>
            <label class="control-label col-sm-1" for="senha">Cargo:</label>
            <div class="col-sm-2">
                <input type="text" class="form-control" id="cargo" name="cargo">
            </div>
            <label class="control-label col-sm-1" for="senha">Privilégio:</label>
            <div class="col-sm-3">
                <select type="text" class="form-control" id="tipo" name="privilegio">
                    <option value="1">Administrador</option>
                    <option value="2">Gerência</option>
                    <option value="3">Analista</option>
                    <option value="4">Assistente</option>
                </select>
            </div>

          </div>

          <br>
          <button type="submit" class="btn btn-default" text-align=center>Cadastrar</button>
        </form>
    </div>
</body>
</html>
  • What a mistake it makes?

  • There is no mistake, only after clicking the sign up button the information disappears from the Form as if the Insert was done in the bank, but when consulting the database the information is not there, that is the most strange.

  • Change your If for if, PHP is case-sensitive.

  • I made those changes now but it still didn’t work.

  • column names in INSERT have repeated name (nome, cpf, email, senha, nascimento, cadastro, setor, cadastro, privilegio) twice register, though the second should be cargo

  • Thank you Leo, this part of the code was really incorrect, but after I fix it still does not record in the bank....

  • Change this line "Erro: " .$sqlusuarios. "<br>" .mysqli_connect_error($conn); for die(mysqli_error($conn)); and post the result. It will be easier for someone to help you.

  • Thanks Andrei, the result of the error was this - "Unknown column 'name' in 'field list'", but I reviewed the code and found nothing incorrect.

  • Guys, solved here, in fact I was doing nonsense...on the Insert line instead of giving the name of the field in the database, I was stating the name of the field in the form.

  • Thank you all very much for your help

Show 5 more comments

1 answer

-1

Suggestions:

  • Add die($sqlusuarios); before calling the function mysqli_query to see if he’s riding the SQL correctly.
  • Look at the directive of PHP, display_erros is enabled.
  • Hello Everton, I did as oriented and theoretically SQL is being mounted correctly, see result. INSERT INTO TB_USUARIOS (name, Cpf, email, password, birth, registration, sector, registration, privilege) VALUES ('Edson Luis Darezzo dos Santos', '30110986890', '[email protected]', '123456', '1981-11-23', '2018-06-15', 'Developer', '1')

  • put at the beginning of your source this block and see what happens when you run. this block will enable full php errors at runtime ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL);

  • Hello Everton, I put this block at the beginning of the code but did not return anything.

Browser other questions tagged

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