Insert data into database according to number of checkboxes

6

I have form where I have fields with chekboxes and I want to know if it is possible to make as many inserts as selected fields. In other words, if the user chooses 4% with% I make 4% with%, is it possible? If so, how can I do it?

Checkboxes code:

    <?
                $result = $connection -> query("select * from produtos");
                while($row = $result -> fetch_array(MYSQLI_ASSOC)){
                    $titulo = $row['titulo'];
                    $numeroproduto = $row['id'];
                ?>
                <input type="checkbox" name="relacionados[]" id="relacionados" value="<?=$id?>" onClick="verificar()"><?=$titulo?><br>
                <?
                }
                ?>

And this is the code where I insert already with the help they gave me, but it does not insert. Can someone help me?

$result = $connection -> query("insert into centrobemestar values(NULL,'$titulo','$texto','$produtos_relacionados', '$pastafinal', '$linguagem')"); $result -> free();

        $id_centrobemestar = $result -> insert_id;      

        $total_opcoes = count($_POST['relacionados']);
        $values =  substr(str_repeat("(NULL,'$id_centrobemestar','?'),", $total_opcoes),0 , -1);
        $sql = 'INSERT INTO tags VALUES '. $values ;

        foreach($_POST['relacionados'] as $item){
            $stmt->bind_param('i', $item);
        }

        $stmt = $connection->prepare($sql);
        if(!$stmt->execute()){
            echo $stmt->error;
        }

I changed the code and the error stopped appearing, now only a warning appears to me. With the following code it already inserts in the database, but everything to zero.

Warning:

  

Warning: mysqli_stmt :: bind_param () [mysqli-stmt.bind-param]: Number of variables does not match number of parameters in prepared statement in /home/devbor/public_html/hausmann/admin/pesquisacentrobemestar.php on line 201

Code:

        $result = $connection -> query("insert into centrobemestar values(NULL,'$titulo','$texto','$produtos_relacionados', '$pastafinal', '$linguagem')"); $result -> free;

        $id_centrobemestar = $result -> insert_id;      

        $total_opcoes = count($_POST['relacionados']);
        $values =  substr(str_repeat("(NULL,'$id_centrobemestar','?'),", $total_opcoes),0 , -1);
        $sql = $connection -> prepare('INSERT INTO tags VALUES '. $values ) ;

        foreach($_POST['relacionados'] as $item){
            $sql->bind_param('i', $item);
        }

        $sql -> execute();
    
asked by anonymous 29.01.2015 / 19:26

2 answers

3

Make a single insert by passing several values , be sure to check for checked items in checkcbox. Then, pass the correct number of fields by string VALUES to bind it, substr remove the last comma of the string.

<?php
$total_opcoes = count($_POST['relacionados']);
$values =  substr(str_repeat('(?),', $total_opcoes),0 , -1);
$sql = 'INSERT  produtos INTO (relacionado) VALUES '. $values ;

$stmt = $connection->prepare($sql);

foreach($_POST['relacionados'] as $item){
    $stmt->bind_param('i', $item);
}


if(!$stmt->execute()){
    echo $stmt->error;
}

The sql will be this more or less that way, I used 5 items as an example and only one field.

INSERT produto INTO (relacionado) VALUES (?),(?),(?),(?),(?)

Related:

Mysqli bind with an array of values

    
29.01.2015 / 19:55
0

It's already resolved. I was having problem with NULL because I do not know how to pass NULL to function bind_param , so I did this:

$result = $connection -> query("insert into centrobemestar values(NULL,'$titulo','$texto','$produtos_relacionados', '$pastafinal', '$linguagem')"); 

        $id_centrobemestar = $connection -> insert_id;
        $result -> free;
        if( count($_POST['relacionados']) ){
            foreach($_POST['relacionados'] as $item)
                $values .= "(NULL,'".(int)$id_centrobemestar."','".(int)$item."'),";
            $result = $connection -> query('INSERT INTO tags VALUES '.substr($values,0,-1) ); $result -> free;
        }
    
30.01.2015 / 15:40