2014-10-22 4 views
1

MySQL을 쿼리하는 PHP 파일에 "국가 코드"를 보내는 드롭 다운 메뉴를 만듭니다. 이렇게하면 html 테이블이 생성됩니다. $ _GET은 항상 아무것도받지 않는다는 점을 제외하면 모든 것이 작동하는 것 같습니다. 심지어 쿼리 문자열에 코드를 하드 코딩 할 때도 마찬가지입니다. 내가 뭘 놓치고 있니?

HTML :

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Test</title> 
    <script type="text/javascript"> 
     function showCountry(str) { 
      if (str == "") { 
       document.getElementById("SurveyContainer").innerHTML = ""; 
       return; 
      } 
      if (window.XMLHttpRequest) { 
       // code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp = new XMLHttpRequest(); 
      } 
      else { // code for IE6, IE5 
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xmlhttp.onreadystatechange = function() { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
        document.getElementById("SurveyContainer").innerHTML = xmlhttp.responseText; 
       } 
      } 

      xmlhttp.open("GET", "getSurveys.php?q=" + str, true); 
      xmlhttp.send(); 
     } 
    </script> 
</head> 
<body> 
    <select name="countries" onchange="showCountry(this.value)"> 
     <option value="">Select Country:</option> 
     <option value="WW">WW</option> 
     <option value="AU">AU</option> 
     <option value="USA">USA</option> 
    </select> 

    <div id="SurveyContainer"><b>Survey table</b></div> 
</body> 
</html> 

getSurveys.php : 사전에

<?php 

//Get Country Code 
$q = (isset($_GET['q']) && is_numeric($_GET['q'])) ? intval($_GET['q']) :'WW'; 

$con = mysqli_connect('localhost','myusername','mypassword','mydatabase'); 

if (!$con) { 
    die('Could not connect: ' . mysqli_error($con)); 
} 

mysqli_select_db($con,"mydatabase"); 

$sql="SELECT * FROM tblSurveys WHERE country = '".$q."'"; 

$result = mysqli_query($con,$sql); 

echo "<table class='surveytable'>"; 
echo "<tr><th width='61'>Country</th><th>Link To Join Paying Survey Panel Web Site</th><th>About Survey Panel</th><th width='93'>Survey Incentives</th></tr>"; 

while($row = mysqli_fetch_array($result)) { 
    echo "<tr><td><b>$row[country]</b></td><td><a href='$row[link]' target='_blank'>$row[name]</a></td><td>$row[description] </td><td>$row[incentives]</td></tr>"; 
} 

echo "</table>";  

mysqli_close($con); 

?> 

감사합니다!

+0

브라우저 URL 표시 줄에서 직접 스크립트를 호출하는 경우 작동합니까? –

+1

참고로 양식 태그가 없습니다. HTML이 유효하지 않습니다. –

+0

'getSurveys.php' 콘텐츠를 수정하십시오. 나는'mypassword '를 의미합니다. –

답변

2

나는 당신이 확인하는 어떤 값을 정확히 모르겠습니다 : $q 또는 $_GET['q']하지만 PHP 스크립트의 상단에있는 논리는 잘못된 것입니다 :

$q = (isset($_GET['q']) && is_numeric($_GET['q'])) ? intval($_GET['q']) :'WW'; 

당신은 숫자 값을 검사하고에 캐스팅 정수이며 검색어 문자열의 값은 아무것도 아니거나 국가 코드 문자열입니다.

실제 $_GET['q'] 값은 스크립트에서 절대로 사용되지 않습니다.

$q = isset($_GET['q']) ? $_GET['q'] :'WW'; 

을 그리고 당신은 SQL 주입을 방지하기 위해 준비된 명령문 (또는 mysqli_real_escape_string()를 ...) 필요 :

는 당신은해야합니다.

+1

모두에게 훌륭한 조언. 나는 모두에게서 조금을 사용했다. 나는 여전히 내가 원하는 결과를 얻지 못하고 있었고 내 캐시가 삭제되지 않은 것을 알기 전까지는 당황 스러웠다. 나는 결코 내 변화를 보지 못했다. DoH! 내 브라우저 캐시를 삭제하고 효과적이었습니다. 그래서 나는 당신의 모든 답변이 도움이되었거나 그렇지 않은지에 대해 확실하지는 않습니다. 그러나 모두에게 감사드립니다! – tversluys

관련 문제