-1

I've got a problem in my PHP code whith an SQL statement.

I want to get back the ID of my Indice where my name = 'myname'. Here is my code :

<?php
include 'Connection.php';

try { 
    $db = new PDO("$server:host=$host;dbname=$base", $user, $passwd); 


    //Statement = INSERT INTO indice
    $stmtInd = $db->prepare("INSERT INTO indice(ID, Name, IDFormation)
        VALUES (:ID, :Name, :IDFormation)");
        $stmtInd->bindParam(':ID', $id);
        $stmtInd->bindParam(':Name', $name);
        $stmtInd->bindParam(':IDFormation', $idformation);
    //Statement = INSERT INTO note
    $stmtNote = $db->prepare("INSERT INTO note(ID, Valeur, Valeurtext, IDIndice)
        VALUES (:ID, :Valeur, :Valeurtext, :IDIndice)");
        $stmtNote->bindParam(':ID', $ID);
        $stmtNote->bindParam(':Valeur', $valeur);
        $stmtNote->bindParam(':Valeurtext', $valeurtext);
        $stmtNote->bindParam(':IDIndice', $IDindice);
    $noteIdindice = $db->prepare("SELECT ID FROM indice WHERE Name = :Name");
        $noteIdindice->bindParam(':Name', $name);
        $noteIdindice->execute();           
        $resultat = $noteIdindice->fetch(\PDO::FETCH_ASSOC);    
        var_dump($resultat);

    //Indice 1
    $name = "Equilibre theorie / pratique";
    $idformation = "1";
    $stmtInd->execute();

    $valeur = $_POST["indice1"];
    $valeurtext = "";
    $IDindice = $resultat['ID'];
    $stmtNote->execute();
    echo "Success";
} 
catch (PDOException $e) { 
    die("Impossible de se connecter a la source de donnees..."); 
}
?>

There is other Indice but you dont need it cuz its the same as "//Indice 1".

Everything works and i have no failure. But my query give me a wrong return. It returns me "0" instead of the ID i want.

Do you guys know why ?

Fefil
  • 3
  • 4

1 Answers1

2

Your prepared statement is never executed, you should add execute :

$noteIdindice = $db->prepare("SELECT ID FROM indice WHERE Name = :Name");
$noteIdindice->bindParam(':Name', $name);
$noteIdindice->execute();//Add this row
$resultat = $noteIdindice->fetch();

EDIT :

You are trying to binding params with null values.

        $stmtInd = $db->prepare("INSERT INTO indice(ID, Name, IDFormation)
        VALUES (:ID, :Name, :IDFormation)");
        $stmtInd->bindParam(':ID', $id);
        $stmtInd->bindParam(':Name', $name); //$name variable not exists
        $stmtInd->bindParam(':IDFormation', $idformation);//$idformation variable not exists

Try to do this :

<?php
include 'Connection.php';

try { 
    $db = new PDO("$server:host=$host;dbname=$base", $user, $passwd); 
    //Indice 1
    $name = "Equilibre theorie / pratique";
    $idformation = "1";
    $valeur = $_POST["indice1"];
    $valeurtext = "";
    //Statement = INSERT INTO indice
    $stmtInd = $db->prepare("INSERT INTO indice(ID, Name, IDFormation)
        VALUES (:ID, :Name, :IDFormation)");
        $stmtInd->bindParam(':ID', $id);
        $stmtInd->bindParam(':Name', $name);
        $stmtInd->bindParam(':IDFormation', $idformation);
        $stmtInd->execute();
    //Statement = INSERT INTO note
    $stmtNote = $db->prepare("INSERT INTO note(ID, Valeur, Valeurtext, IDIndice)
        VALUES (:ID, :Valeur, :Valeurtext, :IDIndice)");
        $stmtNote->bindParam(':ID', $ID);
        $stmtNote->bindParam(':Valeur', $valeur);
        $stmtNote->bindParam(':Valeurtext', $valeurtext);
        $stmtNote->bindParam(':IDIndice', $IDindice);
        $stmtNote->execute(); 
    $noteIdindice = $db->prepare("SELECT ID FROM indice WHERE Name = :Name");
        $noteIdindice->bindParam(':Name', $name);
        $noteIdindice->execute();           
        $resultat = $noteIdindice->fetch(\PDO::FETCH_ASSOC);    
        var_dump($resultat);

    $IDindice = $resultat['ID'];

    echo "Success";
} 
catch (PDOException $e) { 
    die("Impossible de se connecter a la source de donnees..."); 
}
?>
Thomas Rollet
  • 1,573
  • 4
  • 19
  • 33