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();