다음 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')");
}
?>
유일한 문제
, 당신이 볼 수있는이있다가 같은 점수의 여러 인스턴스, 그리고 불가피하게 될 경우, 위치가 뒤죽박죽받을 것입니다. 먼저 점수로 선택하고 합리적인 순서로 위치를 파악할 수 있습니까?편집 : 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)";
를 사용하여 지금 얻을 :
더 나은하지만 위의 점수가 정말 9,8,7,6해야, 5
솔직히 SQL 튀김 내 머리 : P
다음 질문은 전체 파일이 아닌 관련 코드 만 게시하십시오. – svens
분명히 누군가가 같은 점수를 가지고 있다면 그들은 모두 같은 위치에 있습니까? – AndrewC
SQL 삽입 구멍이 있습니다. http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain – Johan