2013-08-10 2 views
0

내 PHP에 & mysql 검색 엔진에 버그가 있습니다.
간단한 검색 엔진 이었지만 필요한 제품의 이미지를 결과에 추가하여이 검색 엔진을 업그레이드하기로 결정했습니다. 따라서 문제는 내가 가지고있는 것입니다. 첫 번째 제품의 이미지는 다음과 같습니다. 다른 모든 제품.검색 엔진 버그

<?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의 모든 회원에게 감사드립니다.

+1

이 부분은'foreach ($ row as $ results) {$ nom = $ row [6]; }'의심스러운 ... – MightyPork

+0

의심스러운 이유는 무엇입니까? –

답변

1

루프 외부에서 이미지 문을 처리하고 있습니다. 첫 번째 루프를 없애고 모든 이미지 처리를 두 번째 루프로 옮깁니다. 첨부 된 샘플 :

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로 전환해야합니다.

+0

PDO를 사용해 보았지만 많은 문제에 직면했습니다. –

+0

mysql_ 함수를 계속 사용하면 더 많은 문제가 발생합니다. –

+0

이 코드는 $ row [6]이 항상 0이기 때문에 아마도 none 이미지 동생 –

2

코드는 SQL-injection에 노출됩니다. 이 같은 것을 사용하십시오 :

$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 코드 here를 작성하는 세부 사항을 제공합니다.

+0

Ok, 감사합니다 –

+1

쿼리의 'escapedSearchQ' 각각에'$'가 누락되었습니다 – Sean

+1

@Sean Thx! 고쳤다. – SteAp