Php does not want to write database data in the database

0
<?php
include_once("conexao.php");
$nome = $_POST['nome'];
$email = $_POST['email'];
$senha = $_POST['senha'];

$sql = ("INSERT INTO 'usuarios'('nome', 'email', 'senha') VALUES ([$nome],[$email],[$senha])");
$result = mysqli_query($conn, $sql);
?>



<html>
<head>
<title>Central de Estudos | Cadastro</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="js/java.js"></script>
<style>
body {
  background-image: url("imgs/bg.jpg");
  background-repeat: no-repeat;
  font-family: "Roboto", sans-serif;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
</style>
</head>


<body>
<div class="login-page">
  <div class="form">
    <h3 style="font-size: 14px;">CENTRAL DE ESTUDOS | CADASTRO</h3>
    <form class="login-form" name="loginform" method="post" action="">
      <input type="text" placeholder="Nome" name="nome"/>
      <input type="email" placeholder="Email" name="email" />
      <input type="password" placeholder="Senha" name="senha" />
      <button type="hidden" name="entrar" value="cadastrar">Cadastrar</button>
      <p class="message"> Desenvolvido por: Marcos A. Massini</p>
    </form>
  </div>
</div>
</body>
</html>
    
asked by anonymous 27.09.2017 / 03:05

1 answer

0

There are at least two errors in your PHP code:

  • Retrieve values in the superglobal $_POST without checking whether the request handled by the server is a POST. That is, when you access the page via the browser, it sends a GET request to the server, so the values in $_POST do not exist, generating an error. If you did not see the error messages, it's possibly because your server is configured for it - which is bad in a development environment: error messages are the developer's best friends, as they tell us what's wrong and pulp time. To get around this, just check the HTTP request method:

    <?php
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
        include_once("conexao.php");
    
        $nome = $_POST['nome'];
        $email = $_POST['email'];
        $senha = $_POST['senha'];
    
        ...
    
    }
    
  • Your SQL query is structured in the wrong way for two reasons:

    • You are using brackets ( [] ) instead of braces ( {} );
    • You are not inserting the quotation marks correctly into the values;
  • The correct form of the query would be:

    $sql = "INSERT INTO 'usuarios' ('nome', 'email', 'senha') VALUES ('{$nome}', '{$email}', '{$senha}')";
    
        
    27.09.2017 / 03:25