首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搜索引擎Bug

搜索引擎Bug
EN

Stack Overflow用户
提问于 2013-08-10 00:43:03
回答 2查看 86关注 0票数 0

我的php&mysql搜索引擎中有一个bug。

这是一个简单的搜索引擎,但我决定通过在结果中添加所需产品的图像来升级这个搜索引擎,所以我遇到的问题是,第一个产品的图像将是所有其他产品的图像。

这里的代码:

代码语言:javascript
复制
<?php
include ('Connections/connect.php');

// collect

if (isset($_POST['search'])) {
    $searchq = $_POST['search'];
    $searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);
    $query = mysql_query("SELECT * FROM tProduct WHERE sProduct LIKE '%$searchq%' OR sSearch LIKE '%$searchq%' OR sSort LIKE '%$searchq%'") or die("La Recherche est impossible");
    $count = mysql_num_rows($query);
    $result = mysql_query("SELECT * FROM tProduct WHERE sProduct LIKE '%$searchq%' OR sSearch LIKE '%$searchq%' OR sSort LIKE '%$searchq%'");
    /*Afichage de L'image*/
    $results = mysql_query("SELECT * FROM tProduct WHERE sProduct='$product'");
    $row = mysql_fetch_row($result);
    foreach($row as $results) {
        $nom = $row[6];
    }

    $dir = "images/";
    if ($row[6] == 0) {
        $image_r = "images/none.png";
    }

    if ($row[6] != 0) {
        if (file_exists($dir . $nom . ".JPEG")) {
            $image_r.= $dir . $nom . ".JPEG";
        }
        else
        if (file_exists($dir . $nom . ".jpg")) {
            $image_r.= $dir . $nom . ".jpg";
        }
        else
        if (file_exists($dir . $nom . ".jpeg")) {
            $image_r.= $dir . $nom . ".jpeg";
        }
    }

    if ($count == 0) {
        $output = "Aucun Résultat Pour Cette Recherche!";
    }
    else {
        while ($row = mysql_fetch_array($query)) {
            $sProduct = $row['sProduct'];
            $output.= '<div><ul><li><a target="_blanc" href="product.php?product=' . $sProduct . '" title="' . $sProduct . '"><img src="' . $image_r . '">' . $sProduct . '</a></li></ul></div>';
        }
    }
}

这是所有的源代码,希望我能找到一些帮助!

感谢stackexchange的所有成员。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-10 00:57:46

您正在处理循环外的图像语句。去掉第一个循环,将所有图像处理移到第二个循环。所附样本:

代码语言:javascript
复制
if (isset($_POST['search'])) {
    $searchq = $_POST['search'];
    $searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);
    $query = mysql_query("SELECT * FROM tProduct WHERE sProduct LIKE '%$searchq%' OR sSearch LIKE '%$searchq%' OR sSort LIKE '%$searchq%'") or die("La Recherche est impossible");
    $count = mysql_num_rows($query);
    $result = mysql_query("SELECT * FROM tProduct WHERE sProduct LIKE '%$searchq%' OR sSearch LIKE '%$searchq%' OR sSort LIKE '%$searchq%'");
    /*Afichage de L'image*/
    $results = mysql_query("SELECT * FROM tProduct WHERE sProduct='$product'");
    // this query looks redundant. your search query should include an image since you're selecting all from that table in both cases

    $dir = "images/";

    if ($count == 0) {
        $output = "Aucun Résultat Pour Cette Recherche!";
    }
    else 
    {
        while ($row = mysql_fetch_array($query)) {
        $image_r = "images/none.png";
        if ($row[6] != 0) {
            $nom = $row[6];
        if (file_exists($dir . $nom . ".JPEG")) {
            $image_r = $dir . $nom . ".JPEG";
        }
        else
        if (file_exists($dir . $nom . ".jpg")) {
            $image_r = $dir . $nom . ".jpg";
        }
        else
        if (file_exists($dir . $nom . ".jpeg")) {
            $image_r = $dir . $nom . ".jpeg";
        }
        }

            $sProduct = $row['sProduct'];
            $output.= '<div><ul><li><a target="_blanc" href="product.php?product=' . $sProduct . '" title="' . $sProduct . '"><img src="' . $image_r . '">' . $sProduct . '</a></li></ul></div>';
        }
    }
}

另外,mysql_functions也不受欢迎。您应该切换到PDO或MySQLi。

票数 1
EN

Stack Overflow用户

发布于 2013-08-10 00:55:24

您的代码易受SQL注入攻击。请用这样的方法:

代码语言:javascript
复制
$searchq        = $_POST['search'];
$searchq        = preg_replace("#[^0-9a-z]#i", "", $searchq);
$escapedSearchQ = mysql_real_escape( $searchq ); 
$query = mysql_query( "SELECT * "
                    . "  FROM tProduct "
                    . " WHERE sProduct LIKE '%" . $escapedSearchQ . "%' "
                    . "    OR sSearch  LIKE '%" . $escapedSearchQ . "%' "
                    . "    OR sSort    LIKE '%" . $escapedSearchQ . "%' "
                    ) 
          or die("La Recherche est impossible")
          ;

OWASP提供了如何编写安全的PHP代码这里的详细信息。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18157554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档