insert array foreach

1

I have a problem with entering data in the database with php, I have a form with several checkboxes, so I want to select more than 1 checkbox and insert it into the database, so I did

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 it into strings but now he wants to insert everything in one line, type 1,2,3 in the category field, and I want it to insert 1, jump to the next line insert 2, and so on

    
asked by anonymous 17.07.2016 / 18:59

1 answer

1

First, do not implode, use the variable $ car 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: code, conteudomsg, ... category Create another example table, Categories: id, category_name

You would have all categories saved in the categories table and would insert into the message.category (foreign key) column 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 value of the category. To avoid this, create another table, for example: message_category message_code, category_id

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

    
18.07.2016 / 03:20