Insert an array within the method call

Asked

Viewed 20 times

0

Colleagues.

I have a form that contains an array that is on the.php sign-up page, see:

<input type="text" name="NomeCliente[]">
<input type="text" name="IdadeCliente[]">
<input type="text" name="CPFCliente[]">

And I have the following method that is inside the methodos.class.php file:

public function cadastrarPaxSingle($nomeCliente,$idadeCliente,$cpfCliente){
             // Aqui faço o cadastramento no banco de dados
}

However, I am registering within the.php registration page in this way:

 for($i = 0; $i < count($nomeCliente); $i++){
             $cadastrar = mysqli_query($conexao,"INSERT INTO tbcadastro VALUES(null,'".$nomeCliente[$i]."','".$idadeCliente[$i]."','".$cpfCliente[$i]."')");

   }

But when I call the method to register within the method:

   $métodos->cadastrarPaxSingle($nomeCliente,$idadeCliente,$cpfCliente);

Register a client and no more. How I would make the code below work?

 public function cadastrarPaxSingle($nomeCliente,$idadeCliente,$cpfCliente){
          for($i = 0; $i < count($nomeCliente); $i++){
               $cadastrar = mysqli_query($conexao,"INSERT INTO tbcadastro VALUES(null,'".$nomeCliente[$i]."','".$idadeCliente[$i]."','".$cpfCliente[$i]."')");
       }
  }
  • and how is Insert?

  • Hi Daniel. I adjusted my question. There is the Internet there.

  • variables in Insert, are not wrong? $nome would not be $nomeCliente, for example?

  • Opa Daniel. Ajustei. I put the code manually and it was a nomenclature error when putting here.

  • the variable $conexao, comes from where?

1 answer

0

Try this one: exchange this "for($i = 0; $i < Count($customer name); $i++)" for this:

foreach($nomeCliente as $nome) {
    $métodos->cadastrarPaxSingle($nome);
}
  • Sorry Fernando, I forgot to put a detail. I edited my question.

Browser other questions tagged

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