나는 약간의 도움이 필요하다. 나는 3 개의 키워드를 기반으로 데이터베이스에서 검색을 수행하는 PHP 스크립트를 가지고 있으며 쿼리는 데이터베이스의 phpadmin에서 훌륭하게 작동하지만 반환 된 JSON 응답은 찾는다. 결과 없음 ("empresa found"). 아무도 왜 내게 말할 수 있습니까? 단일 표 검색을 수행하면 완벽하게 작동합니다. (경우에 그냥 하나 개의 테이블과 관련된 키워드를 사용)PHP 스크립트가 의도 한 JSON 응답을 반환하지 않음
키워드 1 표 A에 이름입니다 키워드 2 키워드 3 표 분야에 부문을위한 테이블 가격
테이블처럼에 PriceRange입니다 :
Table A Table Prices Table Sectors
Name PriceRange Sector ID Pricerange ID Sector
XY 1 2 1 100€ to 200€ 1 Computers
YX 2 1 2 200€ to 300€ 2 Tablets
당신의 코드로
<?php
error_reporting(0);
require_once('db_config.php');
$conn = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
$db= mysql_select_db($db_database, $conn);
header('content-type: application/json; charset=utf-8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
// array for JSON response
$response = array();
// get empresa based on keyword
$keyword=$_GET["keyword"];
$keyword2=$_GET["keyword2"];
$keyword3=$_GET["keyword3"];
if($keyword2 == "Todos os Setores" and $keyword3 == "Qualquer Investimento"):
$result = mysql_query("SELECT * FROM empresa WHERE marca LIKE '%$keyword%' and ficha LIKE '%1' ") or die(mysql_error());
elseif($keyword2 == "Todos os Setores"):
$result = mysql_query("SELECT empresa.ficha, empresa.marca, empresa.empresa, empresa.descricao, empresa.imagempequena from empresa , investimento where investimento.investimento='%$keyword3%' and empresa.nivelinvestimento=investimento.id and empresa.ficha LIKE '%1' and empresa.marca LIKE '%$keyword%'") or die(mysql_error());
elseif($keyword3 == "Qualquer Investimento"):
$result = mysql_query("SELECT empresa.ficha, empresa.marca, empresa.empresa, empresa.descricao, empresa.imagempequena from empresa, sector where sector.sector='%$keyword2%' and empresa.sector=sector.id and empresa.ficha LIKE '%1' and empresa.marca LIKE '%$keyword%'") or die(mysql_error());
else:
//query a funcionar
$result = mysql_query("SELECT empresa.ficha, empresa.marca, empresa.empresa, empresa.descricao, empresa.imagempequena from empresa , investimento, sector where investimento.investimento='%$keyword3%' and empresa.nivelinvestimento=investimento.id and sector.sector='%$keyword2%' and empresa.sector=sector.id and empresa.ficha LIKE '%1' and empresa.marca LIKE '%$keyword%'") or die(mysql_error());
endif;
// check for empty results
if (mysql_num_rows($result) > 0) {
// looping through all results
$response["empresa"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$empresa= array();
$empresa["marca"] = $row["marca"];
$empresa["empresa"] = $row["empresa"];
$empresa["descricao"] = $row["descricao"];
$empresa["imagempequena"] = $row["imagempequena"];
// push single empresa array into final response array
array_push($response["empresa"], $empresa);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No empresa found";
// echo no users JSON
echo json_encode($response);
}
?>
** 경고 ** 귀하의 코드는 SQL 주입 공격에 매우 취약합니다! –
나는 그것을 많이 얻는다. 이 방법이 처음에는 어떻게 작동하는지 이해하고 싶습니다. 어떻게 든 처리 할 수 있습니다. – Acejakk
http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php –