My PHP does not save in DB

0

The connection to the DB was successfully established. If I play the same Query in PhpMyAdmmin the command works and the data is saved in the DB. But if I use the same command in PHP it just does not save in the DB.

Index.php file 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>

Archive cadastro.php 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 spent all day packing this part of making the connection between the source code and MySQL. Thanks in advance for your help.

    
asked by anonymous 20.05.2018 / 23:22

2 answers

2

I made some changes to your code to work.

<?php 
    //conecta com o banco de dados MySQL
    $DB_HOST = "localhost";
    $DB_USUARIO = "root";
    $DB_SENHA = "";
    $DB_BANCO = "crud";

    $db = mysqli_connect($DB_HOST, $DB_USUARIO, $DB_SENHA, $DB_BANCO) or die(mysqli_errno());

    //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.'")';

    $result = mysqli_query($db, $sql);

    if($result)
        echo "<h1>CADASTRADO COM SUCESSO</h1>";
    else
        echo "<h1>FALHA AO CADASTRAR</h1>";
?>

Changes:

1st: I have standardized the connection variables with the bank. The variable $DB_SENHA was written $BD_SENHA .

# 2: At the time of the connection, you were only passing 2 quotes in place of the password, I passed the variable $DB_SENHA .

3rd: I changed double quotation marks to single quotation marks to query SQL. I put it to open and close with single quotation marks and made concatenations to get the values of the $nome, $email e $senha variables and to have double surrounds, since MySQL needs double quotation marks in a insert so that it knows it's a VARCHAR .

Note: Return of a exit(var_dump($sql)) after my changes:

'INSERT INTO user(userNome, userEmail, userSenha) VALUES ("nome", "[email protected]", "senha")'

4 °: I added the SQL "execute", in this case, the line $result = mysqli_query($db, $sql) . The first parameter is your connection to the database, the second parameter is query SQL.

5 °: I placed a checker if the insertion was successful or not. The return of the mysqli_query function is true or false .

I did it here and it worked.

    
20.05.2018 / 23:59
-2

You need to execute the query with the command mysql_query :

<?php
     //query de insercao dos dados do tipo string.
     $sql = "INSERT INTO user(userNome, userEmail, userSenha) VALUES ($nome, $email, $senha)";

     //execucao da query e atribuicao do resultado na variavel $result
     $result = mysql_query($sql);

     //verificando se teve algum retorno e mostrando erro
     if (!$result) {
         die('Invalid query: ' . mysql_error());
     }

?>
    
20.05.2018 / 23:35