2017-05-28 257 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

相关问题