What’s wrong with this mysql data entry code?

Asked

Viewed 167 times

0

I’m trying to make a code to enter data into the database localhost, but I’m having trouble inserting, and I don’t know why.

My database settings are correct and the tables are correct. The error is definitely in the code, so I would like to find out what the problem is:

<?php
include("admin/configs/config.php");
?>
<!DOCTYPE html>
<html>
<head>
    <title>Registro</title>
</head>
<body>
    <div align="center">
        <br><br><br><br><br><br><br>
        <form action="" method="POST" name="form">
            <input type="text" name="r_nome" placeholder="Nome">
            <br><br>
            <input type="text" name="r_usuario" placeholder="Usuário">
            <br><br>
            <input type="password" name="r_senha" placeholder="Senha">
            <br><br>

            <input type="submit" name="submit" value="Registrar">
        </form>
</div>
</body>
</html>
<?php

   if(isset($_POST["submit"])){
        $nome = $_POST["r_nome"];
        $usuario = $_POST["r_usuario"];
        $senha = $_POST["r_senha"];
        $foto = "uploads/";

        if (empty($usuario) || empty($nome) || empty($senha)) {
            echo "Preencha todos os campos!";
    }else{

     $query = "INSERT INTO usuarios (nome, usuario, senha, foto) VALUES ('$nome', '$usuario', '$senha', '$foto')";

  }
}

    ?>
  • You are not running the query.

2 answers

3


I suggest reading the PHP documentation about the class Mysqli and/or PDO

Mysqli

PDO

But basically you need to start the connection to the database, I’m talking in case it doesn’t exist, but for you to understand the sequence:

#dados de acesso ao servidor com o banco de dados
$servidor = "localhost";
$usuario = "root";
$senha = "";
$bancodedados = "seu_banco_de_dados";

#inicia a classe Mysqli
$mysqli = new mysqli($servidor, $usuario, $senha, $bancodedados);    

#verifica a coneção
if ($mysqli->connect_error) {
    die("Falha na conexão: " . $mysqli->connect_error);
}

#sua query
$sql = "INSERT INTO usuarios (nome, usuario, senha, foto) VALUES ('$nome', '$usuario', '$senha', '$foto')";

#execução da query    
if ($mysqli->query($sql) === TRUE) {
    echo "Dados salvos no banco.";
} else {
    echo "Erro: " . $sql . "<br>" . $mysqli->error;
}

#fecha a conexão
$mysqli->close();

Suggestion: For the action on the same page, you can’t even declare this attribute:

<form method="POST" name="form">

0

In

<form action="" method="POST" name="form">

fill in the value of action with the name of your PHP program; if your program is called php program., do

<form action="programa.php" method="POST" name="form">

In PHP code, the database access driver is missing. If you have not yet installed the database server, I recommend the following links,

  • 3

    In my code, I am not using "action" because the code is already in the same file.

Browser other questions tagged

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