【问题标题】:Query problem not working [closed]查询问题不起作用[关闭]
【发布时间】:2011-09-23 06:55:07
【问题描述】:

今天我在定义类别时遇到了这个错误。

类别定义如下:

 $categorie=$_GET['categorie'];    
 if($categorie==nimic)    
 $cat="nimic";    
 elseif($categorie==Aventura)    
 $cat="Aventura";    
 elseif($categorie==Dragoste)    
 $cat="Dragoste";    
 elseif($categorie==Politiste)    
 $cat="Politiste";    
 elseif($categorie==Pentru-Copii)    
 $cat="Pentru-Copii";    
 elseif($categorie==Romanesti)    
 $cat="Romanesti";    
 elseif($categorie==Mistere)    
 $cat="Mistere";    
 elseif($categorie==Scient-Fiction)    
 $cat="Scient-Fiction";    
 elseif($categorie==Vampiri)    
 $cat="Vampiri";    
 elseif($categorie==Thriller)    
 $cat="Thriller";    
 elseif($categorie==Drama)    
 $cat="Drama";    
 elseif($categorie==Erotic)    
 $cat="Erotic";    
 elseif($categorie==Mitologie)    
 $cat="Mitologie";

查询是:

 $query="SELECT *    
 FROM carte    
 WHERE categorie='$cat'    
 AND uploader='$username'    
 ORDER BY data_ad    
 DESC LIMIT $lista";

问题在于前 4 个$cat 正在工作,正在显示可以编辑的结果。但在 $cat 的其余部分,有 0 个文件需要编辑..

在 phpmyadmin 中,我尝试手动检查查询,方法是将 $cat 替换为不工作的类别,并将 $username 替换为在行上传器下填写的有效用户名。

手动运行。那么问题应该出在哪里呢?有什么想法吗?

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    我对你不使用引号的方式感到非常非常不舒服。我会推荐这个:

    $categorie=$_GET['categorie'];
    if($categorie=='nimic')
    $cat="nimic";
    elseif($categorie=='Aventura')
    $cat="Aventura";
    elseif($categorie=='Dragoste')
    $cat="Dragoste";
    elseif($categorie=='Politiste')
    $cat="Politiste";
    elseif($categorie=='Pentru-Copii')
    $cat="Pentru-Copii";
    ...
    

    注意最后一个类别中的元字符(“-”)。只是说... ;)

    【讨论】:

    • 在类别下的 db 中,我已将 2 个类别与“-”分开,查询在“Pentru-Copii”处工作,但在该类别之后,我在下一个类别中显示 0 个结果
    • @Darius - 为什么不采纳 Paul 的建议并在字符串周围使用引号,看看这是否能解决您的问题?
    【解决方案2】:

    首先,$categorie 是什么,$cat 是什么,那么为什么需要该代码?

    其次,字符串应始终使用引号(' 或 ")表示

    这样的东西应该可以覆盖它:

    if (isset($_GET['categorie']) && is_string($_GET['categorie'])) {
        $cat = $_GET['categorie'];
    }
    

    【讨论】:

      【解决方案3】:

      您在常量中使用连字符(我希望它们是常量)。它被解释为减法,即Pentru-CopiiPentru 的值减去Copii 的值。

      我假设来自$_GET['categorie'] 的类别是数字的。最好创建一个数组并使用它来查找值:

      $categories = array(
          264 => 'Aventura',
          621 => 'Dragoste',
          ...
      );
      
      $cat = $categories[ $_GET['categorie'] ];
      

      或者,如果类别始终是一个字符串,您可以使用 $cat = mysql_real_escape_string( $_GET[ 'categorie' ] )

      【讨论】:

      • 绝对正确。这就是我试图在我的帖子中说的。两个问题:a)“破折号==减法”问题(不引用文字),b)为什么复杂的 if/else 块(或 $categories[] 数组) if "_$GET['categorie']"总是 == $cat?
      【解决方案4】:

      任何想法

      曾经尝试打印出您的查询结果并在 phpmyadmin 中运行它?并将其与您在 phpmyadmin 中制作的进行比较?

      在这样的 elseif 列表中也没有任何意义。
      一行

      $cat = mysql_real_escape_string($_GET['categorie']);
      

      够了

      【讨论】:

        【解决方案5】:

        Emm .. 那里有那些常量吗?

        您是否应该代替 if-elseif 巨星做类似的事情:

        $category=$_GET['categorie'];
        $data = array( 
            1 => 'nimic', 
            2 => 'Aventura',
            3 => 'Dragoste',
            4 => 'Politiste',
                etc ...
        );
        
        if ( !array_key_exists( $category , $data )  )
        {
           $category = 1 // set default category
        }
        
        $cat = $data[ $category ];
        

        还有..请学习使用PDO准备好的语句:

        $db = new PDO('mysql:host=localhost;dbname=demo', 'login', 'password');
        $sth = $db->prepare('
           SELECT * FROM carte
           WHERE categorie=:category AND uploader=:user
           ORDER BY data_ad DESC LIMIT :limit'
        );
        $sth->bindParam(':category', $cat, PDO::PARAM_INT);
        $sth->bindParam(':user', $username , PDO::PARAM_STR);
        $sth->bindParam(':limit', $limit, PDO::PARAM_INT);
        $result = $sth->fetchAll();
        

        【讨论】:

        • 在代码中复制数据库内容有什么意义?
        • 整个目录。所有这些 Politiste 和 nimic 都已经在数据库中了。在代码中输入它有什么意义?
        • @col-shrapnel 您在他的主题中哪里读到了?这些类别将在一个单独的表中,如果它们甚至在数据库中。 并且我假设 IF-ELSE 块检查基于常量。
        • @col-shrapnel if($categorie==nimic)$categorie 与常量nimic 进行比较(如果已定义).. 我认为它的存在是有原因的
        • ...如果它没有被定义 - 它将categoriestring nimic 进行比较,我认为它存在只是因为 OP 的语法很弱: )
        【解决方案6】:

        已编辑。

        首先从 MySQL 查询中准备一个数组,然后使用 php 函数搜索该数组:

        $categorie=$_GET['categorie'];
        
        if(in_array($allowedCategories, $categorie)){
          $cat = $categorie;
        }
        

        就是这样。

        【讨论】:

        • 这不是最干净的方式。手动复制数据库内容是没有意义的。
        • 糟糕。没有看到查询。一大早,呵呵:)
        猜你喜欢
        • 1970-01-01
        • 2013-10-05
        • 1970-01-01
        • 1970-01-01
        • 2013-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-02
        相关资源
        最近更新 更多