PHP sends partial data to mysql

0

Oops! I'm learning to use the html + php + mysql trio and the business is giving me a dance. when I finally understood the mistakes I was making, I went for the most real test, I made a very simple login, the strange one, just send the password field to the bank login and the email simply will not go. the codes: cadastro_login.html

<DOCTYPE html>
<meta charset="utf-8">

<html>
		<head>
					<title> Cadastro de Usuário </title>
		</head>

<body>

		<form method="POST" action="cadastro_login.php">
			<label>Nome</label>

			<br>
			<input type="text" name="usuario" >
			<br>

			<br>
			<label>Senha:</label>
			<br>
			<input type="password" name="senha" >

			<br>
			<br>
			<label>e-mail:</label>
			<br>
			<input type="text" name="email" >
			<br>

			<br>
			<input type="submit" value="Cadastrar" name="cadastrar">

		</form>
		<br>
		<br>
		<button type="button" onclick="index.html"> Início</button>
	</body>
</html>

cadastro_login.php

<?php

include_once ('conexao1.php');

$usuario        = $_POST['usuario'];
$senha          = $_POST['senha'];
$email          = $_POST['email'];

$query= "INSERT INTO USUARIOS(usuario, senha, email) 
        VALUES('$usuario','$senha','$email')";

mysqli_query($db, $query);


?>

connection1.php

    <?php

$host = "localhost";
$user = "root";
$password = "";
//$database ="teste";

// Create connection
$db = mysqli_connect($host, $user, $password) or die ('Não foi possivel conectar ao servidor');
        mysqli_select_db($db, 'testa' ) or die(mysqli_error($db));


        echo "conectado";
?>
    
asked by anonymous 26.09.2018 / 19:49

1 answer

0

Good Julian Day,

From a var_dump() in your variable $db , you will probably notice that it does not have any value (NULL), this is because you call the connection file with the conexao1.php database and simply do not use it.

And according to the documentation the mysqli_connect function expects 4 parameters and not 3 missing you provide the name of your database source: link

A tip and you already put together an OOP scheme that would look more like this

class conectaBanco
{
   public $host = "localhost";
   public $user = "root";
   public $passoword = "";

   public function conecta{
   //Seu código

   //Dessa maneira você retorna o seu conector 
   return $db;
   }
}//fim da Classe

//Ai quando você desejar utilizar a conexão basta fazer o seguinte
include_once/require_once ('conexao1.php');

conexao = new conectaBanco(); //Esse é o nome da classe que agente criou anteriormente
$conn = conexao->conecta(); //Estamos acessando o método conecta dentro da classe conectaBanco e "pegamos o seu retorno que no caso é db.

 //dessa maneira conn está "carregando a sua conexão"

 //.. Restante do seu código

 mysqli_query($conn, $query);

I hope this example gives you a glimpse of what needs to be done, I recommend you start taking a look at OOP which is very useful and can greatly facilitate your development.

If you do not know the var_dump that is the php developer's best friend because it facilitates a lot of "debugging" process, follow the documentation link: link and also the documentation regarding POO : link

In case you do not want to develop in POO following the documentation, just connect to the bank you put a return $db; and in the cadastre_login do the following $db= include_once("conexao1.php"); for more details just look at the include_once documentation: link

    
27.09.2018 / 14:53