0
The BD connection has been successfully established. If I play the same Query in Phpmyadmmin the command works and the data is saved in the BD. But if I use the same command in PHP it simply won’t save in BD.
File index.php below:
<form action="cadastro.php" method="post" name="signup">
<div>
<label for="name">Nome: </label>
<input type="text" id="name" name="nome" />
</div>
<div>
<label for="email">E-mail: </label>
<input type="email" id="email" name="email" />
</div>
<div>
<label for="senha">Senha: </label>
<input type="password" id="senha" name="senha" />
</div>
<div class="button">
<button type="submit">Cadastrar!</button>
</div>
</form>
Registration.php file below:
<?php
//conecta com o banco de dados MySQL
$DB_HOST = "localhost";
$DB_USUARIO = "root";
$BD_SENHA = "";
$DB_BANCO = "crud";
$db = mysqli_connect($DB_HOST, $DB_USUARIO, '', $DB_BANCO) or die(mysqli_errno());
?>
<?php
//resgata o que foi mandado pelo usuario
$nome = $_POST ['nome'];
$email = $_POST['email'];
$senha = $_POST['senha'];
//funcao para gravar os dados no banco de dados
$sql = "INSERT INTO user(userNome, userEmail, userSenha) VALUES ($nome, $email, $senha)";
echo "<h1>CADASTRADO COM SUCESSO</h1>";
?>
I was able to learn the concepts of PHP well but I spent all day stuck in this part of making the connection between the source code and Mysql. From now on I appreciate your help.
Is there an error when running? Seeing that you’re starting now, I’ll give you a hint to make it easier to find out where the problem is. Use
exit(var_dump($VariavelQueVocêQuerVerOValor))
to stop the execution and show you what value the variable has at that time. In this case, I would put aexit(var_dump($sql))
just below the line$sql = "INSERT INTO...
to see the value that the variable$sql
has, may not be correct this part here:VALUES ($nome, $email, $senha)
, Maybe you’re not taking the value of the variable$nome
and yes the word $name.– D. Watson
No error appears when running. I used this command you gave me and apparently everything is running normal. Output:
string(109) "INSERT INTO user(userNome, userEmail, userSenha) VALUES (Allyson Henrique, [email protected], 1234)"
I believe there is some problem in the connection between the database and php.– Henrique Marques
I am using Navicat to manage my BD. When I run this Query directly on Navicat it writes to the database. The problem is that Query does not run in PHP, or runs but does not save. I avoid asking in Stackoverflow trivial things, but I could not find any solution to my problem in my researches.
– Henrique Marques
I copied your code and tested it here, got a positive result and put as a response. See if it works in your environment and if it meets you.
– D. Watson