Insert array foreach

Asked

Viewed 569 times

1

am tend a problem when entering data in the database with php , I have a form with several checkbox , then I want to select more than 1 checkbox and insert in the database , I did so

form

<input type="checkbox" name="f_carteira[]" value="'.$cart['id'].'">

action

  $carteira = $_POST['f_carteira'];

  if($carteira){
      foreach ($carteira as $car) {

        $cadastrarmensagem = $pdo->prepare("INSERT INTO mensagem(codigo,conteudomsg,categoria_id,carteira_id,usuario_id) VALUES(:codigo,:conteudo,:categoria,:carteira,:usuarioid)");
  $cadastrarmensagem->bindValue(":codigo",$_POST["f_codigo"]);
  $cadastrarmensagem->bindValue(":conteudo",$_POST["f_msg"]);
  $cadastrarmensagem->bindValue(":categoria",$_POST["f_cat"]);
  $scarteira = implode(",",$carteira);
  $cadastrarmensagem->bindValue(":carteira",$scarteira);
  $cadastrarmensagem->bindValue(":usuarioid",$_POST["f_usuario"]);
  $cadastrarmensagem->execute();
  $linha = $cadastrarmensagem->rowCount();
  if($linha > 0){
     echo "Mensagem Cadastrada com Sucesso";

     header ("Location: ../index.php?pg=mensagens");
  }else {
    echo "Erro";
    //imprimindo erro da variavel de consulta
   print_r($cadastrarmensagem->errorInfo());

    echo "$idlogado";
  }
      }
  }

}else{echo "aconteceu algum erro";}

I was having problems because I was not accepting array , then I used json and turned into strings but now he wants to insert everything in one line , type 1,2,3 in the field of category , and I want him to enter the 1 , jumps to next line inserts the 2 , and so on

  • Why do you want to use a single column to save multiple values? If so, this action violates the so-called "FNF (First Normalization Form)". In short: doing this can work, it can be extremely stable and never present a problem, but it is not recommended. See https://en.wikipedia.org/wiki/First_normal_form for this. You are using a relational database. Create a single table to store categories and associate using the ID. This way create ID | Categoria and relate the ID to the message ID, then just do a JOIN between them, or similar.

  • could help me with a practical example of this, I want you to register a topic I can select in more than one category

1 answer

1

First, don’t implode, use the $car variable that has its category:

  $cadastrarmensagem->bindValue(":categoria",$_POST["f_cat"]);
  //$scarteira = implode(",",$carteira);
  //use a avariavel $car que nao vai mais ser um array
  $cadastrarmensagem->bindValue(":carteira",$car);
  $cadastrarmensagem->bindValue(":usuarioid",$_POST["f_usuario"]);

This will allow your code to work. But it will be complex to manage in the future. Following the previous comment, let’s see: Small example. You have the message table: codigo, conteudomsg, ... category Create another example table, categories: id, category name

You would have all categories saved in the categories table and would insert in the message column.category (foreign key) the number equivalent to the category id marked in the checkbox. Only that the above approach has a problem you will insert the message data several times, changing only the category value. To avoid this create another table, example: messaging code_message, categoria_id

Then your message table would no longer have the category column. You would save the message only once. It would take the code (is it the id of the message table?) and the ids of the categories marked in the check box and would insert n times in the message table_category. Take a look at n for n and n for 1. There are many examples in php.

  • Friend , vlw by the tips , I had tested thought it was working , but he registered only the first record , which may be ?

Browser other questions tagged

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