treehouse : what would you like to learn today?
Web Design Web Development iOS Development

insert with Multi Select multiple values in single attribute

  • Hi everybody I am trying to retrieve data from a multiple select and using PHP. Here is the code creating the select list and populating it with information from a MySQL database:

    <select title="Basic example" multiple="multiple" name="id_categorie[]" size="3">
    <?php
      include_once('../../modeles/class_Categorie.php');
      include_once '../../core/connx.php';
          foreach(Categorie::getAllCategorie()as $cat ){
          $categorie= new Categorie($cat->ID_CAT);
          echo 'getIDCategorie().'">'.$categorie->getNomCategorie().'';
      }
      ?>
    </select> 
    

    The form sends the value using POST method to the URL addArticle.php where I try to use the implode() statement:

    <?php 
      include_once '../core/connx.php';
      include_once('../modeles/class_Article.php');
      include_once('../modeles/class_Image.php');
      include_once '../modeles/class.upload.php';
      $id=Article::creatArticle(0,$_POST['title'],implode(', ', $_POST['id_categorie']),$_POST['article']);
      insertionImage($id,'img1');
      function insertionImage($id,$img){
        $obj_img = new upload('../views/img/file/', $img);
      
        $obj_img->cl_taille_maxi = 1048579900000;
        $obj_img->cl_extensions = array('.gif','.jpg','.png','.jpeg'); // on attribut les extensions autorisées 
        $obj_img->cl_nb_char_aleatoire = 15;
        if (!$obj_img->uploadFichier('aleatoire')){
            $erreur=$obj_img->affichageErreur();
      
        }
        else {
          $chemin=$obj_img->cGetNameFileFinal();
          Image::addImage($id,$chemin);
      
        }
      }
      
      ?>
    

    And the function that adds the article class Article:

    **
     * function créeArticle
     */
    public static function creatArticle($id_article,$title,$id_categorie,$article)
    {
        global $db;
    
        $req = $db->prepare("INSERT INTO blog (ID_BLOG,TITLE,ID_CAT, ARTICLE,DATE) VALUES ('',:title,:id_categorie,:article,'".date('Y-m-d')."')");
        $ok = $req->execute(Array('title' =>$title,'id_categorie' => $id_categorie,'article' => $article));
         return $db->lastInsertId();
        //$erreur = $req->errorInfo();
    }
    

    The problem is when I select multiple values, I only get the first selected option from the form. How can I get all selected values !? Thanx