2011-10-04 2 views
0

다음 PHP 및 MySQL 코드를 사용하여 사용자 점수보다 5 위와 5 위에있는 데이터베이스에서 최고 점수 기록을 끌어냅니다. 2 개의 기준으로 SQL 행을 주문하고 잡아야합니다.

<?php 

$dbcon = mysql_connect("localhost", "XXXX", "XXXX") or  die(mysql_error()); 
mysql_select_db("tulesblo_koreangame", $dbcon) or die(mysql_error()); 
mysql_query("SET NAMES utf8"); 

$name = $_POST["name"]; 
$email = $_POST["email"]; 
$score = $_POST["score"]; 
$table = ""; 
$submit = ""; 
$input = ""; 
$newposition = $_POST['position']; 

$position = mysql_query("(SELECT position 
    FROM highscore 
    WHERE score < '$score' 
ORDER BY score DESC 
    LIMIT 1)"); 

if(!$name){ 

$gethigherrows = "(SELECT * 
    FROM highscore 
    WHERE score >= '$score' 
ORDER BY score ASC 
    LIMIT 5)"; 

$getlowerrows = "(SELECT * 
    FROM highscore 
    WHERE score < '$score' 
ORDER BY score DESC 
    LIMIT 5)"; 

$higherrows= mysql_query($gethigherrows); 
$lowerrows= mysql_query($getlowerrows); 

if(mysql_error())echo mysql_error(); 

while($row=mysql_fetch_array($higherrows)) 
{ 
    $uppertable .= "<tr><td>$row[position]</td><td>$row[name]</td> <td>$row[score]</td></tr>"; 
} 

$x = 0; 
if (mysql_num_rows($lowerrows) > 0) 
{ mysql_query("UPDATE highscore SET position = position + 1 WHERE score < '$score'")or die("update failed"); 
    while($row=mysql_fetch_array($lowerrows)) 
    { 
     if ($x == 0) 
      {$position = $row['position'];}; 
     $x++; 
     $newpos = $row[position]+1; 
     $lowertable.= "<tr><td>$newpos</td><td>$row[name]</td> <td>$row[score]</td></tr>"; 
    } 
    $input = "<tr><td id='position'>$position</td><td><input id='nameinput'type='text' /></td><td>$score</td></tr>"; 
    $submit = "<br />Enter email if you want to receive a prize!<br /><input id='emailinput'type='text' /><br /><input id='submithighscore'type='submit' value='Submit'>"; 
} 

$table .= "<table id='scoretable'><tr><th>Position</th><th>Name</th><th>Score</th></tr>"; 
$table .= $uppertable; 
$table .= $input; 
$table .= $lowertable; 
$table .= "</table>"; 
$table .= $submit; 
$table .= "<br /><span class='msg'></span>"; 
echo $table; 

}else{ echo($newposition); 
mysql_query("INSERT INTO highscore VALUES (NULL, '$score', '$name', '$email',  '$newposition')"); 
} 

?> 

screen shot

유일한 문제

, 당신이 볼 수있는이있다가 같은 점수의 여러 인스턴스, 그리고 불가피하게 될 경우, 위치가 뒤죽박죽받을 것입니다. 먼저 점수로 선택하고 합리적인 순서로 위치를 파악할 수 있습니까?

편집 : OK, 다음

$gethigherrows = "(SELECT * 
    FROM highscore 
    WHERE score >= '$score' 
ORDER BY 
position ASC 
    LIMIT 5)"; 

$getlowerrows = "(SELECT * 
    FROM highscore 
    WHERE score < '$score' 
ORDER BY score DESC, 
position DESC 
    LIMIT 5)"; 

를 사용하여 지금 얻을 :

enter image description here

더 나은하지만 위의 점수가 정말 9,8,7,6해야, 5

솔직히 SQL 튀김 내 머리 : P

+0

다음 질문은 전체 파일이 아닌 관련 코드 만 게시하십시오. – svens

+0

분명히 누군가가 같은 점수를 가지고 있다면 그들은 모두 같은 위치에 있습니까? – AndrewC

+2

SQL 삽입 구멍이 있습니다. http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain – Johan

답변

3

당신과 특이 수 y ORDER BY 절에있는 둘 이상의 열.

ORDER BY score ASC, position DESC 
+0

그래, 적어도 시각화 한 것 같습니다. 가장 낮은 (가장 높은 숫자) 위치를 가진 사용자보다 높은 점수를 가진 5 개의 레코드를 잡아서 결과가 적절하게 순차적 일 것입니다. 내가 무슨 뜻인지 알 겠어? – Tules

+0

비슷한 : ---------- SELECT * FROM highscore WHERE score> '$ score'및 위치 (그룹에서 가장 높은 숫자 임) – Tules

+0

실제로는 아닙니다. '.. $ WHERE score> {$ score} ORDER BY 위치 DESC'와 같은 것? – svens

2

먼저 나는 그 SQL 주입 구멍을 해결하는 것입니다 :

$score = mysql_real_escape_string($_POST['score']); 
$name = mysql_real_escape_string($_POST['name']); 
$email = mysql_real_escape_string($_POST['email']); 

$sql = "(SELECT * 
     FROM highscore 
     WHERE score >= '$score' 
     ORDER BY score ASC, position DESC 
     LIMIT 5)"; 

당신은 내가 점수 ' or (1=1) LIMIT 1 UNION SELECT password, email, username FROM users --을 substitude 수없는 경우. 사용자의 모든 비밀번호와 이메일 주소 목록을 얻으려면.

+0

감사드립니다. – Tules

관련 문제