What's wrong with this code to insert data into mysql?

0

I am trying to make a code to insert data into the localhost database, however I am not able to insert, and I do not know why.

My database settings are correct and the tables are also correct. The error for sure is 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')";

  }
}

    ?>
    
asked by anonymous 03.06.2018 / 23:35

2 answers

3

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

  

Mysqli

     

PDO

But basically you need to start the connection to the database, I'm talking in case it does not exist, but 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();

Tip: For action on the same page, you can not even declare this attribute:

<form method="POST" name="form">
    
03.06.2018 / 23:47
0

In

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

fill in the value of action with the name of your PHP program; if your program calls programa.php , 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 bank server, I recommend the following links,

03.06.2018 / 23:44