My PHP not saved in BD

Asked

Viewed 234 times

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 a exit(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.

  • 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.

  • 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.

  • 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.

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

Amendments:

1°: I standardized the connection variables with the database. The variable $DB_SENHA was written $BD_SENHA.

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

3°: I changed from double quotes to single quotes a query SQL. I put to open and close with simple quotes and made concatenations to get the values of the variables $nome, $email e $senha and get double quotes around, since Mysql needs double quotes in one insert to let him know that he is 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 "executor", in this case, the line $result = mysqli_query($db, $sql). The first parameter is your connection to the bank, the second parameter is the query SQL.

5°: I put a checker whether it worked out or not. Function return mysqli_query is true or false.

I made it here and it worked.

  • Whoa, buddy! Thanks for your attention. I managed to run the code and write to the BD using this concatenation the way you did! It’s a different way than any I’ve seen before. I’ve learned a lot from your help. Thank you for the motivation and the attention!

-2

Missing execute query with 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());
     }

?>
  • it wasn’t I who denied your answer but the AP uses mysqli so it would be $result = mysqli_query($db, $sql); and see also http://php.net/manual/en/mysqli.error.php

  • I hadn’t noticed, vlw!

Browser other questions tagged

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