2012-02-20 66 views
0

我是一名iphone程序员,在php和mysql中并不那么出色。我的问题是,我有一个在线高分系统,在我的所有模式中都有3张桌子。我试图根据来自GET变量的输入来选择表,但它不起作用。这是我在php中的代码。根据GET变量选择表格

$typeGame = isset($_GET['type']) ? $_GET['type'] : ""; 
    $typeGame = mysql_real_escape_string($type); 
    if ($typeGame === "Sprint") { 
$table = "highscoresSprint"; 
    } 
    else if ($typeGame === "Normal") 
    { 
    $table = "highscoresNormal"; 
    } 
    else 
    { 
    $table = "highscoresMarathon"; 
    } 

其中typeGame是请求的游戏类型,$ table是表格。

我希望你能帮助我,它就是行不通的。

干杯,乔。

+0

任何错误输出张贴这件事看到的事情吗? 'echo $ _GET ['type']'返回什么?并且url必须是'http://test.com/game?type = Sprint'类似的东西。 – Alex 2012-02-20 19:06:23

+0

echo $ typeGame =? – 2012-02-20 19:08:36

+0

@ Furgas的回答应该这样做 – Alex 2012-02-20 19:09:15

回答

3

尝试:的

$typeGame = mysql_real_escape_string($typeGame); 

代替:

$typeGame = mysql_real_escape_string($type); 
+0

+1,噢,我没有看到那一个。 – Alex 2012-02-20 19:08:51

+0

谢谢兄弟,这解决了我的问题,我会在4分钟内接受它。谢谢!! – coderjoe 2012-02-20 19:14:54

0
$typeGame = isset($_GET['type']) ? $_GET['type'] : ""; 
//You don't need this - >$typeGame = mysql_real_escape_string($type); 
if ($typeGame == "Sprint") { // Change to == instead of === 
$table = "highscoresSprint"; 
} else if ($typeGame == "Normal") // Change to == instead of === 
{ 
$table = "highscoresNormal"; 
} 
else 
{ 
$table = "highscoresMarathon"; 
} 
+1

为什么不是===?我的个人规则:当您确定预期的类型时,使用===(或!==)。 – Furgas 2012-02-20 19:13:19

+0

好点@Furgas。另外我认为使用'mysql_real_escape_string'是适当的,因为它是一个可以通过URL修改的SQL字符串。 – 2012-02-20 19:16:10

+0

但这不是传递给他的查询的变量,至少这不是我读它的方式。我没有看到需要明确地比较类型,我通常不用字符串。但是你还是要说一个好点的Furgas,也许我会开始实施它 – romo 2012-02-20 19:19:49

0
$typeGame = mysql_real_escape_string($type); 

我猜你想说:

$ typeGame = mysql_real_escape_string($ typeGame);

0

Furgas答案将解决这个问题,但我对别人的嵌套在切换会这样,所以才想

switch (
    mysql_real_escape_string(
     isset($_GET['type']) ? $_GET['type'] : '' 
    ) 
) { 
    case 'Sprint': 
     $table = "highscoresSprint"; 
     break; 
    case 'Normal': 
     $table = "highscoresNormal"; 
     break; 
    default: 
     $table = "highscoresMarathon"; 
     break; 
}