2017-09-03 1 views
-1

Je suis actuellement en train de mettre en place un système de suivi utilisant PHP et PDO. Le code détermine si le bouton "Suivre" ou "Ne plus suivre" devrait s'afficher en fonction des données insérées. La base de données est mise à jour correctement, mais pour une raison quelconque, l'opérateur supérieur ou égal à ne fonctionne pas correctement ou je n'interroge pas correctement la base de données. (Je ne sais pas lequel) Quelqu'un sait ce que je fais mal?PHP/PDO Supérieur ou égal à l'opérateur après la requête

En followon.php:

if($row['userID'] && $row['userName']){ 
    if($row['userID']!=$user_id){ 
     $follow_userid = $row['userID']; 

     $stmt = $user_follow->runQuery("SELECT id FROM following WHERE user1_id=':user_id' AND user2_id=':follow_userid'"); 
       $stmt->execute(array(":user_id"=>$user_id,":follow_userid"=>$follow_userid)); 
       $follow = $stmt->fetch(PDO::FETCH_ASSOC); 

       if(!$follow >= 1){ 

      $stmt = $user_follow->runQuery("INSERT INTO following(user1_id, user2_id) VALUES (?, ?)"); 
       $stmt->bindValue(1,$user_id); 
       $stmt->bindValue(2,$follow_userid); 
       $stmt->execute(); 

      $stmt = $user_follow->runQuery("UPDATE tbl_users SET following = following + 1 WHERE userID = ?"); 
       $stmt->bindValue(1,$user_id); 
       $stmt->execute(); 

      $stmt = $user_follow->runQuery("UPDATE tbl_users SET followers = followers + 1 WHERE userID = ?"); 
       $stmt->bindValue(1,$follow_userid); 
       $stmt->execute(); 

       } 

     header("Location: index.php?id=".$currentID); 
     } 
} 

En followoff.php:

if($row['userID'] && $row['userName']){ 
    if($row['userID']!=$user_id){ 
     $unfollow_userid = $row['userID']; 

     $stmt = $user_unfollow->runQuery("SELECT id FROM following WHERE user1_id=':user_id' AND user2_id=':unfollow_userid'"); 
       $stmt->execute(array(":user_id"=>$user_id,":unfollow_userid"=>$unfollow_userid)); 
       $follow = $stmt->fetch(PDO::FETCH_ASSOC); 

       if($follow >= 1){ 

      $stmt = $user_unfollow->runQuery("DELETE FROM following WHERE user1_id= ? AND user2_id= ?"); 
       $stmt->bindValue(1,$user_id); 
       $stmt->bindValue(2,$unfollow_userid); 
       $stmt->execute(); 

      $stmt = $user_unfollow->runQuery("UPDATE tbl_users SET following = following - 1 WHERE userID = ?"); 
       $stmt->bindValue(1,$user_id); 
       $stmt->execute(); 

      $stmt = $user_unfollow->runQuery("UPDATE tbl_users SET followers = followers - 1 WHERE userID = ?"); 
       $stmt->bindValue(1,$unfollow_userid); 
       $stmt->execute(); 

       } 

     header("Location: index.php?id=".$currentID); 
     } 
} 

Et dans index.php (où le bouton apparaît):

if($user_id){ 
       if($user_id!=$id){ 

        $query2 = $user_home->runQuery("SELECT id FROM following WHERE user1_id=':user_id' AND user2_id=':id'"); 
             $query2->execute(array(":user_id"=>$user_id,":id"=>$id)); 
             $query2result = $query2->fetch(PDO::FETCH_ASSOC); 

        if($query2result >= 1){ 
               echo "<a href='followoff.php?id=$currentID' class='btn btn-default btn-xs'>Unfollow</a>"; 

        } 
        else{ 

         echo "<a href='followon.php?id=$currentID' class='btn btn-info btn-xs'>Follow</a>"; 

        } 
       } 
      } 
+1

'$ query2result' est __array__. Comment ça peut être plus grand que 1? Que comparez-vous ici? –

+1

Vous utilisez des instructions préparées avec des valeurs d'espace réservé, et c'est très bien, mais n'oubliez pas que les espaces réservés * ne devraient pas avoir de guillemets *. Ceux-ci sont ajoutés par le pilote de base de données s'ils sont jugés nécessaires. – tadman

Répondre

1

que vous essayez de tester le jeu de résultats avec un ID, $query2->fetch renvoie un tableau associatif.

Vous devez accéder au champ dans le jeu de résultats ...

if($query2result['id'] >= 1){ 

Si vous voulez juste dire si une ligne n'a pas été retourné, il retournera false.

if($query2result !== false){ 
+0

Aucune de ces solutions ne semble fonctionner pour moi. – Cordell

+1

Avez-vous fait 'print_r ($ query2result);' pour voir ce qu'il contient. –