how to make two inwords in php

Asked

Viewed 39 times

-2

Hello, I’m trying to make two inserts in php like this:

function insereProduto($conexao, $nome, $cor, $preco){
    $sql="insert into produtos (nome, cor) values ('{$nome}','{$cor}')");
     $sql2="insert into preco (preco) value ({$preco});");
    return mysqli_query($conexao, $sql. ";". $sql2);
}



What am I doing wrong? Thanks in advance.

  • 3

    Something is very strange in your code. After you insert 2 products or more, how do you know which price is of which product? If this was a past exercise, it is the recommendation of who drafted the review methodology. Exercise with too artificial condition harms the student.

1 answer

1


You only need a connection string, in case the $sql will look like this:

$sql = "insert into produtos ...; insert into preco ...;";
mysqli_multi_query($conexao, $sql);

Your error was referring the two querys inside multi_query, to make it right you should assign the two values to a single query.

See in the example below how multi_query receives its parameters and note that it recognizes only one string:

mysqli_multi_query ( mysqli $link , string $query )

For more details about mysql multi_query: https://www.php.net/manual/en/mysqli.multi-query.php

  • Thanks a friend, it worked

  • 2

    The error was only the fact of not using multi_query. The concatenation of the question has no problem, but the function. It would have worked with $sql. ";". $sql2 no problem if the questioner had used the proper function.

Browser other questions tagged

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