Salut Viki et merci
Oui dans la BDD il y a qu’un seul enregistrement concernant mon id user ainsi que l’id de la célébrité.
Voici mon code html:
<?php require 'inc/requetes_scientifiquefou.php' ;?>
<?php require 'inc/header.php' ;?>
<?php require 'inc/db.php' ;?>
<?php logged_only();?>
<?php $insert = $scientifiquefou ->celebrity_root?>
<section class ="find -dishes ">
<h1 >Choisissez la cat égorie de plats </h1 >
<article >
<?php foreach ($list_category as $category ):?>
<div id ="<?php echo $category ->id_category ?> "class ="categories ">
<a title ="<?= $category ->category_name ?>" href ="<?=$scientifiquefou ->celebrity_root .'_ '.$category ->id_category .$ext_page ?>">
<img alt ="<?= $category ->category_name ?>" src ="<?= 'img /'.$category ->category_root .$ext_jpg ?>"></a >
<h3 ><a title ="<?= $category ->category_name ?>"href ="<?=$scientifiquefou ->celebrity_root .'_ '.$category ->id_category .$ext_page ?>">
<?= $category ->category_name ?></a ></h3 >
</div >
<?php endforeach ; ?>
</article >
<h2 class ="titre ">Liste des Entr ées </h2 >
<div class ="fixnav ">
<img class ="round " src ="<?=$dir_img_celebrities .$scientifiquefou ->celebrity_root .$ext_png ?>" alt ="<?= $scientifiquefou ->celebrity_name ?>">
<h2 >Liste des Entr ées </h2 >
<img class ="round1 " src ="<?=$dir_img_celebrities .$scientifiquefou ->celebrity_root .$ext_png ?>" alt ="<?= $scientifiquefou ->celebrity_name ?>">
</div >
<aside >
<img class ="computer " src ="<?=$dir_img_celebrities .$scientifiquefou ->celebrity_root .$ext_png ?>" alt ="<?= $scientifiquefou ->celebrity_name ?>">
<!-- <img class ="favorite -dishes1 " src ="<?=$dir_img_celebrities .$scientifiquefou ->favorite_dish1 .$ext_jpg ?>" alt ="Plat Favori " >
<img class ="favorite -dishes2 " src ="<?=$dir_img_celebrities .$scientifiquefou ->favorite_dish2 .$ext_jpg ?>" alt ="Plat Favori " >
<img class ="favorite -dishes3 " src ="<?=$dir_img_celebrities .$scientifiquefou ->favorite_dish3 .$ext_jpg ?>" alt ="Plat Favori " >-->
</aside >
<article >
<form class ="list -dishes " method ="POST " action ='insert <?php echo $insert ?>1.php '>
<?php foreach ($list_entrees_scientifiquefou as $entree ):
$classDivChoix = "";
$yesChecked ="";
$noChecked ="";
$neitherChecked ="";
if ($userscientifiquefou1 ) {
switch ($entree ->choix){
case "oui" :
$classDivChoix = "found-checked" ;
$yesChecked = 'checked="checked"' ;
break ;
case "non" :
$classDivChoix = "not-found-checked" ;
$noChecked = 'checked="checked"' ;
break ;
default :
$classDivChoix = "neither-checked" ;
$neitherChecked = 'checked="checked"' ;
break ;
}
}
?>
<div id="<?php echo $entree ->id_dish ?>" class ="dishes <?php echo $classDivChoix ;?>">
<h4 ><?php echo $entree ->dish_name ?></h4 >
<img src ="<?php echo $dir_img_dishes .$list_category [0]->category_root .'/'.$entree ->dish_root .$ext_jpg ?>" alt ="<?php echo $entree ->dish_name ?>" >
<div id ="<?php echo $entree ->id_dish ?>" class ="form -check ">
<div class ="yes -no ">
<input class ="form -check -input " id ="<?php echo $entree ->dish_name ?>" name ="<?= $entree ->id_dish ?>" type ="radio " value ='oui ' <?php if ($userscientifiquefou1 ) {if ($entree ->choix=='oui' ){ echo 'checked="checked"' ;}}?> <?php echo $yesChecked ;?> >Oui
<label class ="form -check -label " for =""></label >
</div >
<div class ="yes -no ">
<input class ="form -check -input " id ="<?php echo $entree ->dish_name ?>" name ="<?= $entree ->id_dish ?>" type ="radio " value ='non ' <?php if ($userscientifiquefou1 ) {if ($entree ->choix=='non' ){ echo 'checked="checked"' ;}}?> <?php echo $noChecked ;?> >Non
<label class ="form -check -label " for =""></label >
</div >
<div class ="dont -know ">
<input class ="form -check -input " id ="<?php echo $entree ->dish_name ?>" name ="<?= $entree ->id_dish ?>" type ="radio " value ='ne_sais_pas ' <?php if ($userscientifiquefou1 ) {if ($entree ->choix=='ne_sais_pas' ){ echo 'checked="checked"' ;}}else {echo 'checked="checked"' ; }?> <?php echo $neitherChecked ;?> >Je ne sais pas
<label class ="form -check -label " for =""></label >
</div >
</div >
</div >
<?php endforeach ; ?>
<div class ="frame ">
<button class ="custom -btn btn -5"><span >Enregistrer </span ></button >
</div >
</form >
</article >
</section >
<?php require 'inc /footer .php ';?>
Voici mon code php du formulaire :
<?php
require_once 'inc/requetes_scientifiquefou.php' ;
require_once 'inc/functions.php' ;
require 'inc/db.php' ;
logged_only();
$id = 6 ;
$location = $scientifiquefou ->celebrity_root;
if (!empty ($_POST )){
if ($userscientifiquefou1 ){
$choix =($_POST );
$id_session = $_SESSION ['auth' ]->id;
$req = $pdo ->prepare('UPDATE dishes_found SET choix=? WHERE dish_id = ? AND celebrity_id=' .$id .' AND user_id=' .$id_session );
foreach ($choix as $key =>$value ){
$req ->execute([$value , $key ]);
}
$_SESSION ['flash' ]['success' ] = "L'enregistrement a bien été pris en compte" ;
header ('Location: ' .$location .'_1.php' );
exit ();
} else {
$choix =($_POST );
$id_session = $_SESSION ['auth' ]->id;
$req = $pdo ->prepare('INSERT INTO dishes_found SET choix = ? , dish_id = ?, celebrity_id = ?, user_id = ?, category_id =?' );
foreach ($choix as $key =>$value ){
$req ->execute(array ($value , $key , $id , $id_session , 1 ));
};
header ('Location: ' .$location .'_1.php' );
$_SESSION ['flash' ]['success' ] = "L'enregistrement a bien été pris en compte" ;
exit ();
};
} else {
$errors ['choix' ] = "l'enregistrement n'a pas pu etre pris en compte" ;
exit ();
}
Et enfin voici mon script de requetes pour cette célébrité :
<?php
require 'functions.php' ;
require 'db.php' ;
logged_only();
?>
<?php
$sql = $pdo ->prepare("SELECT id_dish,dish_name,dish_root,cafeland_dishes.category_id,category_name,category_root,dishes_found.user_id,dishes_found.choix,dishes_found.celebrity_id,celebrity_name,celebrity_root\n"
. "FROM dishes_found\n"
. "INNER JOIN cafeland_dishes ON dishes_found.dish_id = cafeland_dishes.id_dish\n"
. "INNER JOIN cafeland_category ON dishes_found.category_id=cafeland_category.id_category\n"
. "INNER JOIN cafeland_celebrities ON dishes_found.celebrity_id=cafeland_celebrities.id_celebrity\n" );
$sql ->execute();
$cafeland = $sql ->fetchAll();
$dir_img_celebrities = 'img/celebrites/' ;
$dir_img_dishes = 'img/dishes/' ;
$ext_jpg = '.jpg' ;
$ext_png = '.png' ;
$ext_page = '.php' ;
if (isset ($_POST ['choix' ])){
$choix = $_POST ['choix' ];
}
$query = $pdo ->query('SELECT * FROM cafeland_celebrities' );
if ($query === false ){
var_dump($pdo ->errorInfo());
die ('Erreur SQL' );
}
$list_celebrities = $query ->fetchAll(PDO::FETCH_OBJ);
$query = $pdo ->query('SELECT * FROM cafeland_category' );
if ($query === false ){
var_dump($pdo ->errorInfo());
die ('Erreur SQL' );
}
$list_category = $query ->fetchAll(PDO::FETCH_OBJ);
$sql = $pdo ->prepare("SELECT id_dish,dish_name,dish_root,cafeland_dishes.category_id,category_name,category_root,dishes_found.user_id,dishes_found.choix,dishes_found.celebrity_id,celebrity_name,celebrity_root\n"
. "FROM dishes_found\n"
. "INNER JOIN cafeland_dishes ON dishes_found.dish_id = cafeland_dishes.id_dish\n"
. "INNER JOIN cafeland_category ON dishes_found.category_id=cafeland_category.id_category\n"
. "INNER JOIN cafeland_celebrities ON dishes_found.celebrity_id=cafeland_celebrities.id_celebrity\n"
. "WHERE celebrity_id=6" );
$sql ->execute();
$scientifiquefou = $sql ->fetch();
if (!empty ($scientifiquefou )){
$sql = $pdo ->prepare("SELECT id_dish,dish_name,dish_root,cafeland_dishes.category_id,category_name,category_root,dishes_found.user_id,dishes_found.choix,dishes_found.celebrity_id,celebrity_name,celebrity_root\n"
. "FROM dishes_found\n"
. "INNER JOIN cafeland_dishes ON dishes_found.dish_id = cafeland_dishes.id_dish\n"
. "INNER JOIN cafeland_category ON dishes_found.category_id=cafeland_category.id_category\n"
. "INNER JOIN cafeland_celebrities ON dishes_found.celebrity_id=cafeland_celebrities.id_celebrity\n"
. "WHERE celebrity_id=6" );
$sql ->execute();
$scientifiquefou = $sql ->fetch();
} else {
$sql = $pdo ->prepare("SELECT * FROM cafeland_celebrities WHERE id_celebrity=6" );
$sql ->execute();
$scientifiquefou = $sql ->fetch();
};
$sql = $pdo ->prepare("SELECT id_dish,dish_name,dish_root,cafeland_dishes.category_id,category_name,category_root,dishes_found.user_id,dishes_found.choix,dishes_found.celebrity_id,celebrity_name,celebrity_root\n"
. "FROM dishes_found\n"
. "INNER JOIN cafeland_dishes ON dishes_found.dish_id = cafeland_dishes.id_dish\n"
. "INNER JOIN cafeland_category ON dishes_found.category_id=cafeland_category.id_category\n"
. "INNER JOIN cafeland_celebrities ON dishes_found.celebrity_id=cafeland_celebrities.id_celebrity\n"
. "WHERE celebrity_id=6 AND cafeland_dishes.category_id=1 AND dishes_found.user_id =" .$_SESSION ["auth" ]->id);
$sql ->execute();
$userscientifiquefou1 = $sql ->fetch();
if ($userscientifiquefou1 ){
$sql = $pdo ->prepare("SELECT id_dish,dish_name,dish_root,cafeland_dishes.category_id,category_name,category_root,dishes_found.user_id,dishes_found.choix,dishes_found.celebrity_id,celebrity_name,celebrity_root\n"
. "FROM dishes_found\n"
. "INNER JOIN cafeland_dishes ON dishes_found.dish_id = cafeland_dishes.id_dish\n"
. "INNER JOIN cafeland_category ON dishes_found.category_id=cafeland_category.id_category\n"
. "INNER JOIN cafeland_celebrities ON dishes_found.celebrity_id=cafeland_celebrities.id_celebrity\n"
. "WHERE celebrity_id=6 AND cafeland_dishes.category_id=1" );
$sql ->execute();
$list_entrees_scientifiquefou = $sql ->fetchAll();
} else {
$sql = $pdo ->prepare("SELECT * FROM cafeland_dishes WHERE category_id= 1" );
$sql ->execute();
$list_entrees_scientifiquefou = $sql ->fetchAll();
}
et voici le resultat de ma bdd
bdd.PNG