我正在尝试从多个选择中检索数据并使用PHP。下面是创建选择列表并使用MySQL数据库中的信息填充它的代码:
<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 '<option value="'.$categorie->getIDCategorie().'">'.$categorie->getNomCategorie().'</option>';
}
?>
</select>
表单使用POST
方法将值发送到网址addArticle.php
,我尝试使用implode()
语句:
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);
}
}
添加文章类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();
}
问题是当我选择多个值时,我只从表单中获取第一个选中的选项。如何获取所有选定的值?