insert multiple records checkbox mysql

0

I'm trying to insert information into multiple checkbox information at once, for example a news item is related to more than one category, so I just select the categories and save, but this is giving this error,

Notice: Array to string conversion in C:\Program Files\VertrigoServ\www\painel\pg\cadamsg.php on line 19
ErroArray ( [0] => HY000 [1] => 1366 [2] => Incorrect integer value: 'Array' for column 'carteira_id' at row 1 ) 
Notice: Undefined variable: idlogado in C:\Program Files\VertrigoServ\www\painel\pg\cadamsg.php on line 32

Notice: Array to string conversion in C:\Program Files\VertrigoServ\www\painel\pg\cadamsg.php on line 19
ErroArray ( [0] => HY000 [1] => 1366 [2] => Incorrect integer value: 'Array' for column 'carteira_id' at row 1 ) 
Notice: Undefined variable: idlogado in C:\Program Files\VertrigoServ\www\painel\pg\cadamsg.php on line 32

Notice: Array to string conversion in C:\Program Files\VertrigoServ\www\painel\pg\cadamsg.php on line 19
ErroArray ( [0] => HY000 [1] => 1366 [2] => Incorrect integer value: 'Array' for column 'carteira_id' at row 1 ) 
Notice: Undefined variable: idlogado in C:\Program Files\VertrigoServ\www\painel\pg\cadamsg.php on line 32

Notice: Array to string conversion in C:\Program Files\VertrigoServ\www\painel\pg\cadamsg.php on line 19
ErroArray ( [0] => HY000 [1] => 1366 [2] => Incorrect integer value: 'Array' for column 'carteira_id' at row 1 ) 
Notice: Undefined variable: idlogado in C:\Program Files\VertrigoServ\www\painel\pg\cadamsg.php on line 32

Notice: Array to string conversion in C:\Program Files\VertrigoServ\www\painel\pg\cadamsg.php on line 19
ErroArray ( [0] => HY000 [1] => 1366 [2] => Incorrect integer value: 'Array' for column 'carteira_id' at row 1 ) 
Notice: Undefined variable: idlogado in C:\Program Files\VertrigoServ\www\painel\pg\cadamsg.php on line 32

There were several errors because there are all checked checkboxes there is the code

<?php
                  foreach ($carteira as $cart) {
                     echo '
                    <div class="checkbox i-checks">
                      <label>
                        <input type="checkbox" name="f_carteira[]" value="'.$cart['id'].'">
                        <i></i>
                      '.$cart["nome"].'
                      </label>
                    </div> ' ;
                  }
                        ?>

insert

<?php
          session_start();
 include '../config/config.inc.php';

        error_reporting(E_ALL);


if (isset($_POST["submit"])) {


     $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"]);
      $cadastrarmensagem->bindValue(":carteira",$_POST["f_carteira"]);
      $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";}
 ?>
    
asked by anonymous 15.07.2016 / 03:51

3 answers

2

When you do <input type="checkbox" name="f_carteira[]" what comes POST is an array and the conversion from array to string is the word Array . What you can do is give an implode to convert to a string with the categories.

$aCategoria = ['1','2','3'];
$sCategorias = implode(",",$aCategoria);
//resultando em 1,2,3

But since the error is Incorrect integer value , and you want to save multiple categories, it might be best to use another table to save the categories, where you save the id item and id categories for each of the item categories.

    
15.07.2016 / 04:10
1

As quoted by our partner Fernando, an implode can solve your problem, making everything a string and then you can insert.

This would work perfectly, but particularly, I'd rather use JSON instead of a string with commas. In your case the two would work, however, if one of the values of the array was another array, it would not work with just one implode.

Example with JSON:

<?php 
$bolso = array('seda', 'bic', 'green');
$json = json_encode($bolso);
// $json é igual a uma string: ["seda", "bic", "green"]

Then you can work with the data again as follows:

<?php
$json = '["seda", "bic", "green"]';
$bolso = json_decode($json, true);
//bolso é igual a um array: array('seda', 'bic', 'green')

It is important to remember that if the second argument of json_decode () is not true, instead of returning an array, it will return an object (so it works the same way in many ways, but for example, a foreach does not would work if it were an object).

    
15.07.2016 / 19:29
-1

Good people, none of the situations worked right with json doing so

 $carteira = $_POST['f_carteira'];
 $jsoncart = json_encode($carteira);

returned this error:

Invalid argument supplied for foreach()

and with implode

 $carteira = $_POST['f_carteira'];
 $scarteira = implode(",",$carteira);

Invalid argument supplied for foreach ()

    
16.07.2016 / 18:32