Php does not want to write registration data to the database

Asked

Viewed 30 times

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>
  • In $sql, swap out the [] for {}. To confirm if this is really the problem, just check the value of $sql before and after the change.

  • I changed it, but it still wasn’t

  • My connection is like this.

  • <?php $server = "localhost"; $user = "root"; $password = ""; $dbname = "register"; $Conn = Mysqli_connect($server, $user, $password, $dbname) or print (mysql_error()); if(!Mysqli_connect($server, $user, $password)) { echo "Error when connecting"; } ?>

  • And what was the result when displaying the value of $sql? It seems that quotes are missing in the values there too.

  • None, it shows no error, just does not save the data in mysql.

  • It is not the mistake, but rather putting one var_dump($sql) in your code. And you are already in contact with me here.

  • So I had understood something else, he printo me this:

  • string(105) "INSERT INTO usuarios(nome, email, senha) VALUES (Marcos Augusto Masini, [email protected],81810338)"

  • just correct your query to $sql = "INSERT INTO usuarios (nome, email, senha) VALUES ('$nome', '$email', '$senha')"; that will work.

Show 5 more comments

1 answer

0

There are at least two errors in your PHP code:

  1. Recover 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 browser, it sends a GET request to the server and thus the values in $_POST do not exist, generating an error. If you didn’t display the error messages, it’s possibly because your server is set up for this - which is very bad in a development environment: the error messages are the developer’s best friend, because they tell us what’s wrong and the time pulp. 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'];
    
        ...
    
    }
    
  2. Your SQL query is incorrectly structured for two reasons:

    • Is using clasps ([]) in place of keys ({});
    • Not properly inserting quotes in values;

The correct form of the query would be:

$sql = "INSERT INTO `usuarios` (`nome`, `email`, `senha`) VALUES ('{$nome}', '{$email}', '{$senha}')";
  • Opaaaaaaaaaaa, thanks <3, I’m learning PHP now, so you’ve seen kkk, always when I do something like I use : if ($_SERVER["REQUEST_METHOD"] == "POST")

  • Only when it makes sense: handle HTTP requests with POST method. If this didn’t make sense, I recommend studying the HTTP protocol before studying PHP.

  • Okay, thank you very much

Browser other questions tagged

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