2017-06-10 22 views
1

私はこの問題を自分で解決しようとしましたが、私は固執しています。私はPHPのブログサイトで働いています。私は記事にコメントを追加したいときにエラーが発生します:未定義の変数:クエリエラー//

未定義変数:C:\ wamp \ www \ entertheletter.dev \ fonctions \ blog.php on line 73

どこがうまくいかないのか分かりません。送信ボタンをクリックすると起こります。ここに私のコード:

PHPはエラーを示している機能:

function recherche(){ 
      global $bdd; 

      extract($_POST); 

      $recherche = $bdd->prepare("SELECT id, titre, accroche, publication, image FROM articles WHERE titre LIKE :query OR contenu LIKE :query ORDER by id DESC"); 
      $recherche->execute([ 
       "query" => '%' . $query . '%' 

      ]); 
      $recherche = $recherche->fetchAll(); 

      return $recherche; 
     } 

機能コメントする:私はあなたが$_POSTためextractを使用してはならないと思います

function commenter() { 
      if(isset($_SESSION["membre"])) { 
       global $bdd; 

      $erreur = ""; 

      extract($_POST); 

      if(!empty($commentaire)) { 
       $id_article = (int)$_GET["id"]; 

       $commenter = $bdd->prepare("INSERT INTO commentaires(id_membre, id_article, commentaire) VALUES(:id_membre, :id_article, :commentaire)"); 
       $commenter->execute([ 
        "id_membre" => $_SESSION["membre"], 
        "id_article" => $id_article, 
        "commentaire" => nl2br(htmlentities($commentaire)) 
        ]);  

       } 
       else 
        $erreur .= "Vous devez écrire du texte"; 

       return $erreur; 
      } 

     } 

おかげ

+0

extract($_POST)使用$_POSTを使用していますか? –

+0

"query" => '%'。 $ query。 '%' – imbecco

+0

は '$ query'は' $ _POST'から来ますか?もしそうなら、 '$ _POST'に' $ _POST ['query'] ' –

答えて

0

をか$_GETキーが$_POSTに存在しない場合、PHPは未定義のエラーをスローします。また、$_POSTのデータが存在するかどうかを確認する必要があります。

代わりのライン73に何があるか、直接

function recherche(){ 
    global $bdd; 

    if(isset($_POST['query'])){ 

     $recherche = $bdd->prepare("SELECT id, titre, accroche, publication, image FROM articles WHERE titre LIKE :query OR contenu LIKE :query ORDER by id DESC"); 
     $recherche->execute([ 
      "query" => '%' . $query . '%' 

     ]); 
     $recherche = $recherche->fetchAll(); 

     return $recherche; 
    } 
}