The inclusion of data is not occurring in the database, and the connection and variable is arriving in the function

Asked

Viewed 70 times

-1

Function gravar_task($connection, $task) { if(mysqli_connect_error($connection)){

    echo 'Sem conexão com banco de dados';

    die();
}
$sqlGravar = " INSERT INTO tarefas (nome, descricao, prioridade) VALUES ( '{$tarefa['nome']}', '{$tarefa['descricao']}', '{$tarefa['prioridade']}' ) ";
mysqli_query($conexao, $sqlGravar);

}

  • Well at first you can try to capture the same error made with the connection ... if (!mysqli_query($conexao, $sqlGravar);) {
 printf("Erro: %s", mysqli_error($conexao));
}

  • Thanks, I hadn’t seen your comment. But with it I found the solution to the problem. It was in Html that was passing the wrong value.

2 answers

1


        <?php
$conexao = mysqli_connect("localhost","roo","","test");
$tarefa = array("nome"=>"a", "descricao"=>"b", "prioridade"=>"c");

gravar_tarefa($conexao, $tarefa);

function gravar_tarefa($conexao, $tarefa){ 
    $nome = 'nome'; 
    $descricao = 'descricao'; 
    $prioridade = 'prioridade'; 

    if(mysqli_connect_error($conexao)){

        echo 'Sem conexão com banco de dados';

        die();
    }

for ($i=0; $i < count($tarefa); $i++) { 
$sqlGravar = <<<INSERT
    INSERT INTO teste (nome, descricao, prioridade) VALUES ( '$tarefa[nome]', '$tarefa[descricao]', '$tarefa[prioridade]' )
INSERT;
$result = mysqli_query($conexao, $sqlGravar);
}

    if($result==true){
        echo "Dados Salvos";
    }else{
        echo "dados não salvos";
    }

}
  • Thanks, but the data that is included is not fixed is passed by parameter in an array

  • I added the array see if it helps you

  • Test did not work, then I ran the test $result = mysqli_query($connection, "INSERT INTO tasks (name, description, priority) VALUES ('cu','mouse','fur' )"); if ($result){ echo "worked"; }Else{ echo "didn’t work"; } ? > and with <<<INSERT and tbm not working can be the database and not the code ???

  • 1

    yes can be your table put the structure of it

  • Thank you so much for helping me.

  • Reply tag as useful ;)

Show 1 more comment

1

Hello

I tested it with Raul Fernando’s code and it worked normal. Make sure your database is properly created and the tables match your code. Tested in PHP 7.

  • Thank you so much for helping me.

Browser other questions tagged

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