Questionnaire avec php : rien ne s'ajoute à ma base

Asmaeab Messages postés 7 Statut Membre -  
jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   -
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>QCM</title>
<meta charset="utf-8" />
<link rel="icon" href="./photos/anim2.gif" type="image/gif">
<style type="text/css">
body {background-color: #A8BBE6;}a {color: blue;}a:hover {color: #AB6100;}a:link {color: brown;}
</style>
</head>

<body>

<?php
include ("connexion.php");

if(isset($_POST['choice1']) && isset($_POST['choice2']) && isset($_POST['choice3']) &&isset($_POST['choice4']) && isset($_POST['choice5']) && isset($_POST['choice6']) && isset($_POST['choice7']) && isset($_POST['mail'])){
$choice1 = intval($_POST['choice1']);
$choice2 = intval($_POST['choice2']);
$choice3 = intval($_POST['choice3']);
$choice4 = intval($_POST['choice4']);
$choice5 = intval($_POST['choice5']);
$choice6 = intval($_POST['choice6']);
$choice7 = intval($_POST['choice7']);
$mailconnect= intval($_POST['mail']);
$c1=0;
$c2=0;
$c3=0;
$c4=0;
$c5=0;
$c6=0;
$c7=0;
$résultat=0;
$message_resultat="";
$processus="";
if($choice1 == 1 || $choice1==5) {$c1 = 0;}
elseif($choice1==2) {$c1=0.3;}
elseif($choice1 == 3) {$c1 = 0.7;}
elseif($choice1 == 4) {$c1 = 1;}
if($choice2 == 1 || $choice2==5) {$c2 = 0;}
elseif($choice2==2){ $c2=0.3;}
elseif($choice2 == 3) {$c2 = 0.7;}
elseif($choice2 == 4) {$c2 = 1;}
if($choice3 == 1 || $choice3==5) {$c3 = 0;}
elseif($choice3==2) {$c3=0.3;}
elseif($choice3 == 3) {$c3 = 0.7;}
elseif($choice3 == 4) {$c3 = 1;}
if($choice4 == 1 || $choice4==5) {$c4 = 0;}
elseif($choice4==2) {$c4=0.3;}
elseif($choice4 == 3) {$c4 = 0.7;}
elseif($choice4 == 4) {$c4 = 1;}
if($choice5 == 1 || $choice5==5) {$c5 = 0;}
elseif($choice5==2) {$c5=0.3;}
elseif($choice5 == 3) {$c5 = 0.7;}
elseif($choice5 == 4) {$c5 = 1;}
if($choice6 == 1 || $choice6==5) {$c6 = 0;}
elseif($choice6==2) {$c6=0.3;}
elseif($choice6 == 3) {$c6 = 0.7;}
elseif($choice6 == 4) {$c6 = 1;}
if($choice7 == 1 || $choice7==5) {$c7 = 0;}
elseif($choice7==2) {$c7=0.3;}
elseif($choice7 == 3) {$c7 = 0.7;}
elseif($choice7 == 4) {$c7 = 1;}

$résultat=(($c1*3+$c2*3+$c3*3+$c4*3+$c5*3+$c6*3+$c7*3)/21)*100;
$processus="Processus : Définition de l'architecture SI";
$message_resultat= 'Votre résultat de conformité suite à l audit fait le'.date('Y-m-d H:i:s'). 'est de :'. $résultat .'%' ;
echo $processus;
echo $message_resultat;
$bdd = new PDO ('mysql:host=localhost;dbname=espace_membre;charset=utf8', 'root', '');
$reponse1 = $bdd->exec('INSERT INTO reponses VALUES($resultat,$processus,$mailconnect)');

}

?>
</body>
</html>

Bonjour j'ai un problème avec ce code avec les deux dernières lignes, rien ne s'ajoute à ma base

3 réponses

  1. Asmaeab Messages postés 7 Statut Membre
     
    ça marche maintenant, un problème dans la requete
    0
  2. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
     
    Bonjour,

    1 - En général ... on place son code php AVANT le code HTML (donc avant la balise <html ..

    2 - Tu as un fichier : connexion.php ... je suppose donc qu'il contient déjà les infos de connexion à la bdd ... alors pourquoi avoir réécrit le code de connexion dans ton code avant le INSERT ??

    3 - On récupère PROPREMENT les variables AVANT de les utiliser.
    Pour cela on va utiliser l'écriture ternaire et se servir de la fonction ISSET ou de la fonction !EMPTY

    4 - Pour la connexion... au lieu de faire un
    include ("connexion.php");
    je te conseille de faire un
    require_once "connexion.php";


    5 - Il faut, si ce n'est pas déjà fait dans ton fichier connexion.php ... activer les erreurs PDO :
    comme ici : https://forums.commentcamarche.net/forum/affich-37584941-php-pdo-gerer-les-erreurs

    6 - Il faut mettre les requêtes dans des blocs TRY/CATCH (voir lien du point 6 )

    7 - Vu que tu fais du PDO... fais le bien et utilise donc les requêtes préparées !

    0
  3. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
     
    Voici les corrections effectuées...
    NB : Tu remarqueras que j'ai fais une "fonction" pour le calcul vu que tu utilisais toujours les mêmes critères...

    Ton fichier de connexion à la bdd
    <?php
    try{
    $bdd = new PDO ('mysql:host=localhost;dbname=espace_membre;charset=utf8', 'root', '');
    // Activation des erreurs PDO
     $bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // mode de fetch par défaut : FETCH_ASSOC / FETCH_OBJ / FETCH_BOTH
     $bdd->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    } catch(PDOException $e) {
        die('Erreur : ' . $e->getMessage());
    }
    ?>
    


    Le reste de ton code :
    <?php
    //-------------------------------------//
    // Affichage des erreurs PHP
    //-------------------------------------//
    error_reporting(E_ALL);
    ini_set('display-errors','on');
    
    //-------------------------------------//
    //connexion à la bdd
    //-------------------------------------//
    require_once "connexion.php";
    
    //-------------------------------------//
    // fonction pour le calcul des notes :
    //-------------------------------------//
     function getNote($choicexx){
      if($choicexx == 1 || $choicexx==5) {
        $c = 0;
      }elseif($choicexx==2) {
        $c=0.3;
      }elseif($choicexx == 3) {
        $c = 0.7;
      }elseif($choicexx == 4) {
       $c = 1;
      }
      return $c;
     }
    
    //-------------------------------------//
    //récupération PROPRE des variables
    //-------------------------------------//
      $choice1 = !empty($_POST['choice1']) ? intval($_POST['choice1']) : NULL;
      $choice2 = !empty($_POST['choice2']) ? intval($_POST['choice2']) : NULL;
      $choice3 = !empty($_POST['choice3']) ? intval($_POST['choice3']) : NULL;
      $choice4 = !empty($_POST['choice4']) ? intval($_POST['choice4']) : NULL;
      $choice5 = !empty($_POST['choice5']) ? intval($_POST['choice5']) : NULL;
      $choice6 = !empty($_POST['choice6']) ? intval($_POST['choice6']) : NULL;
      $choice7 = !empty($_POST['choice7']) ? intval($_POST['choice7']) : NULL;
      $mailconnect = !empty($_POST['mail']) ? trim($_POST['mail']) : NULL;
      
    if( $choice1 &&
        $choice2 &&
        $choice3 &&
        $choice4 &&
        $choice5 &&
        $choice6 &&
        $choice7 &&
        $mail
      ){
      
      $c1=getNote($choice1);
      $c2=getNote($choice2);
      $c3=getNote($choice3);
      $c4=getNote($choice4);
      $c5=getNote($choice5);
      $c6=getNote($choice6);
      $c7=getNote($choice7);
      $résultat=0;
      
      $resultat=(($c1*3+$c2*3+$c3*3+$c4*3+$c5*3+$c6*3+$c7*3)/21)*100;
      $processus="Processus : Définition de l'architecture SI";
      $message_resultat= "Votre résultat de conformité suite à l'audit fait le ".date('Y-m-d H:i:s'). " est de :". $resultat ."%" ;
    
      //-------------------------------------//
      // Traitement du formulaire -> requete INSERT
      //-------------------------------------//
    
      //préparation de la requête et des variables
      //normalement on spécifie les champs dans lesquels ont fait l'insertion.. comme ceci :
      //$sql = "INSERT INTO matable (champ1,champ2) VALUES (:valeur1,:autrevaleur)"; 
      $sql = 'INSERT INTO reponses  VALUES(:resultat,:processus,:mailconnect)';
      $datas = array(':resultat'=>$resultat,':processus'=>$processus,'mailconnect'=>$mailconnect);
    
      //Execution de la requete
      try{
        $requete = $bdd -> prepare($sql) ;
        $requete->execute($datas) ;
      }catch(Exception $e){
        // en cas d'erreur :
        echo " Erreur ! ".$e->getMessage();
        echo " Les datas : " ;
       print_r($datas);
      }
    
    }//fin du if
    ?>
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
    <head>
    <title>QCM</title>
    <meta charset="utf-8" />
    <link rel="icon" href="./photos/anim2.gif" type="image/gif">
    <style type="text/css">
    body {background-color: #A8BBE6;}a {color: blue;}a:hover {color: #AB6100;}a:link {color: brown;}
    </style>
    </head>
     
    <body>
    <?php
      echo  $processus;
      echo $message_resultat;
    ?>
    </body>
    </html>
    
    


    edit: petite correction ... on ne met JAMAIS de caractères accentués dans le nom d'une variable !!! on écrit : $resultat et non pas $résultat !

    Cordialement, 
    Jordane                                                                 
    0