-1

so I attempt to insert the values of my checkbox into my database but I can't.. Can you help me please ?

This is the PHP Code

<?php
 
if(empty($_POST["choix"]))
{
echo "Il faut préciser le type d'abonnement";

}
else{
    
include "connectBdd.php";
try {
$sql="INSERT into abonee(nomabo,prenomabo,villeabo, adressmail_abo,adress_abo,abotel,codepostalbo) values(:nom,:prenom,:ville,:mail,:perso,:tel,:code)";
$resultat = $cnx->prepare($sql);
$nbLignes= $resultat->execute(array(":nom"=> $_POST["nom"],
":ville"=> $_POST["ville"],
":mail"=> $_POST["mail"],
":perso"=> $_POST["perso"],
":tel"=> $_POST["numero"],
":code"=> $_POST["code"],
":typeAbo"=>$_POST["choix"]) 
":prenom"=> $_POST["prenom"]));
 
 
echo $nbLignes." ligne ajoutée";

}
catch(PDOException $e) { // gestion des erreurs
echo"ERREUR dans l'ajout ".$e->getMessage();
}

}
?>

The problem here is that I generally use a foreach loop to extract the values of a checkbox but in this situation I can't because we are in a declaration of a array

Thank you everyone !

Edit : this is the content of my $_POST

array(9) { ["choix"]=> array(2) { [0]=> string(15) "Abonnée Papier" [1]=> string(19) "Abonnée numérique" } ["Don"]=> string(2) "ab" ["prenom"]=> string(10) "first name" ["nom"]=> string(10) " last name" ["ville"]=> string(4) " Usa" ["mail"]=> string(0) "" ["perso"]=> string(16) " blabla@gmai.com" ["code"]=> string(3) "123" ["numero"]=> string(3) "156" }

I can post the HTML form too in order to understand ?

Your Common Sense
  • 154,967
  • 38
  • 205
  • 325
Wiam
  • 1
  • 1
  • Please post content of your $_POST, `var_dump($_POST)` – Slava Rozhnev May 09 '22 at 10:15
  • What do you mean, "can't"? And why you're asking "how to insert" if you already have a code that does it? – Your Common Sense May 09 '22 at 10:20
  • welcome to stackoverflow wiam. have you take a [tour] and learn [ask]? i cant seem to find this "checkbox" you mentioned, where is it? does [this qa on how to post multiple checkbox](https://stackoverflow.com/a/4997271) help you? – Bagus Tesa May 09 '22 at 10:25
  • Ahh I'm really sorry to mention this but I made a checkbox in "typeAbo" and when I run my code it works but when I see the database it shows me "Array" instead of the values checked by the person. – Wiam May 09 '22 at 11:02
  • You **aren't** going to store all the the values checked by the person into a single column, are you? – Your Common Sense May 09 '22 at 11:13
  • I edit my code in order to see the content of my $_POST too ! – Wiam May 09 '22 at 11:13
  • 1
    `choix` is an array, you can't do `":typeAbo"=>$_POST["choix"]`, you also are closing the array for the `execute too soon there. You could `implode` the values but that will make the schema an issue in the future. Likely you should have two tables. Store `choix` values in second table with id pointing the first table (`abonee`) – user3783243 May 09 '22 at 11:17
  • Well here the field "typeAbo" allows to see what type of subscription a person has, either it's a digital subscriber or a paper subscriber and as it can be both, I wanted to make a field that stores these values there Otherwise no all the values are stored on different fields obviously – Wiam May 09 '22 at 11:20
  • Ahh yes I can do that, in the worst case I can make 2 different fields like "paper subscriber" and "digital subscriber" because if I create another table it can make unnecessary joins – Wiam May 09 '22 at 11:25
  • @Wiam What if you need a third, fourth, or fifth subscription type? Having a second table is the correct approach. A second column could work but is the incorrect approach. What's wrong with using a `join`? – user3783243 May 09 '22 at 11:26
  • In this case I only need 2 subscription type but yeah I'll test your option ! – Wiam May 09 '22 at 11:29
  • Therefore, we cannot store the values in a checkbox in a database using PDO ? – Wiam May 09 '22 at 11:31
  • checkbox belongs to **HTML** and PDO works with a **database**. They have absolutely nothing in common. Even in your code there is no "checkbox" but only a php array. – Your Common Sense May 09 '22 at 11:33
  • Obviously, PDO can store any value. You just have to make your mind what values to store. – Your Common Sense May 09 '22 at 11:34
  • I'm sorry, I edited my post and I added my HTML form ! – Wiam May 09 '22 at 11:39
  • There is no HTML when you work with PDO. No checkbox. You already have a PHP **array** and have to work with PHP array. Your question is how to insert an array. You shouldn't mention any HTML with PDO. They never meet in the code – Your Common Sense May 09 '22 at 11:43
  • You have three solutions. In order of proper approaches they are 1. Two tables with identifier in second table pointing to first https://en.wikipedia.org/wiki/First_normal_form 2. Have two columns in your table and populate when the value is checked, have default be NULL or 0. 3. Mush the data together in a mess that will most likely me unusable in the future, `implode` (this really shouldn't even be considered an option)... there are threads/tutorials on all these approaches already as well – user3783243 May 09 '22 at 11:56
  • The problem with the second option is that a subscriber ( yeah the table is called subscriber in french lmao )is necessarily a paper or digital subscriber, so it can't have a null value in this field, but thanks to all the people who could help me, but yeah I know that the third option kinda sucks lmao – Wiam May 09 '22 at 13:27
  • @Wiam Make both columns have default value be 0, if it is checked set it to 1. – user3783243 May 09 '22 at 14:13
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community May 09 '22 at 19:34

0 Answers0