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.– Inkeliz
could help me with a practical example of this, I want you to register a topic I can select in more than one category
– Thalles Honorato