2017-05-28 130 views
0

我目前正在使用以公式檢查配方的projet。我接近尾聲,但我真的不明白我的錯誤是什麼。如果有人能向我解釋:致命錯誤:未捕獲的PDOException:SQLSTATE [42S22]:未找到列

Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column '$idingredient' in 'where clause' in /homepages/30/d675437312/htdocs/assets/recettemalin/recetteselectionnee.php:55 Stack trace: #0 /homepages/30/d675437312/htdocs/assets/recettemalin/recetteselectionnee.php(55): PDO->query(' SELECT DIST...') #1 {main} thrown in /homepages/30/d675437312/htdocs/assets/recettemalin/recetteselectionnee.php on line 55

這裏是我的代碼在那裏塊:

if (isset($_POST['ingredients'])) { 
     $idingredient = implode(",", $_POST['ingredients']); 
     echo $idingredient.'<br>'; 
     $recette = $bdd->query(' SELECT DISTINCT r.* 
            FROM ingredient i 
            INNER JOIN contient c 
             ON i.id_i = c.id_i 
            INNER JOIN recette r 
             ON c.id_r = r.id_r 
            WHERE i.id_i IN ($idingredient)'); 
    while ($donnees = $recette->fetch()) { 
     echo $donnees['nom_r']; 
     echo $donnees['type_r']; 
     echo $donnees['description']; 
    } 
} 

當我回聲$idingredient我得到這樣成分的ID列表:6,9,11,12 ,1,5但似乎不被條款所喜歡,我在想爲什麼?

回答

0

您應該使用「,而不是」在查詢字符串來評估變量:

$recette = $bdd->query(" SELECT DISTINCT r.* 
            FROM ingredient i 
            INNER JOIN contient c 
             ON i.id_i = c.id_i 
            INNER JOIN recette r 
             ON c.id_r = r.id_r 
            WHERE i.id_i IN ($idingredient)"); 

或者你可以探微concatinate變量字符串:

'.....' . $idingredient . '...'; 
0

這太長了

首先,請參數化您的查詢,不要只是將隨機字符串轉儲到查詢中,因爲它可能會導致語法錯誤

其次,您的具體問題是您正在放入一個字符串,但沒有字符串分隔符。因此,該值被解釋爲一個標識符 - 特別是列別名。我不推薦這種解決方案,但WHERE i.id_1 IN ('$idingredient')可能做你想要的。

第三,不要使用IN來比較單個值。這是誤導。此外,該值是單個值,即使它包含逗號。 。 。 'butter,margarine'是一個有逗號的值,不是兩個值。

+0

'('。$ idingredient。')' – hassan

相關問題