PHP sends partial data to mysql

Asked

Viewed 64 times

0

opa ! I am learning to use the trio html+php+mysql and the business is giving me a ball. when I finally managed to understand the mistakes I was making, I went to the most real test, I made a very simple login registration, the strange, only sends the password field to the bank the login and the email simply won’t. 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>

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";
?>

1 answer

0

Good Morning Juliano ,

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

And according to the documentation the function mysqli_connect wait 4 parameters and not 3 missing you provide the name of your source database:http://php.net/manual/en/function.mysqli-connect.php

A tip and you already set up a POO scheme that would be more like less 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 is a clear one of what should be done , I recommend you start to take a look at the POO which is very useful and which can greatly facilitate its development.

In case you don’t know the var_dump who is the best friend of the php developer because it facilitates in much process of "debbugar" follows the link of the documentation: http://php.net/manual/en/function.var-dump.php and also documentation in relation to POO: http://php.net/manual/en/language.oop5.php

But in case you don’t want to develop in POO following the documentation just in conecta_banco you put a return $db; and in registration do the following $db= include_once("conexao1.php"); for more details just take a look at the documentation of the include_once: https://secure.php.net/manual/en/function.include-once.php

  • Thanks for the tips ! I will analyze calmly ...

  • the strange thing is that after I restarted it worked normal ... at first I’ll leave as it is, as I’m not a programmer and I won’t be, I’m only doing it because I’ve tried 3 people and the three said they would and never spoke to me again, I’ll try to make it as simple as possible.

Browser other questions tagged

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