2010-07-27 287 views
-1

我碰到了一些与我的项目有关的问题。像许多书呆子一样,我决定创建自己的视频游戏评论网站。评论存储在数据库中,并且可以通过游戏的标题检索通过URL:处理'。'通过url传递给MySQL查询的字符

http://www.example.com/reviews/ {gameName}/{可选 pageOfReview}

不幸的是,测试的边缘情况时,我来到发生奇怪的错误 - 如果游戏在标题中有一段时间,我无法检索它。如果这段时间是标题的主角(如.hack),我会得到一个Kohana堆栈跟踪错误屏幕,告诉我评论(或者更确切地说,游戏)不存在。如果它位于标题的中间或末尾,我会收到一条自己的错误消息,说明无法检索评论(游戏)。有没有办法解决?这是MySQL解析一段时间或其他问题的问题吗?

编辑:所有查询都通过Kohana 2的ORM功能使用MySQLi驱动程序进行处理。节能审查(管理控制器):

public function saveReview() 
{ 
    $this->checkAdmin(); 

    if (isset($_POST['submit'])) { $this->storeReview(); } 
    else { header('Location: /admin'); } 
} 

private function storeReview($id = null) 
{ 
    if (!preg_match("/^[a-zA-Z0-9\-_:!'. ]*$/", $_POST['gameTitle']) || empty($_POST['gameTitle'])) { $gameTitle = false; } 
    else { $gameTitle = ucwords($this->clean($_POST['gameTitle'])); } 

    if (!is_numeric($_POST['genre'])) { $genre = false; } 
    else { $genre = $_POST['genre']; } 

    $platformCheckArray = array_map('is_numeric', $_POST['platforms']); 

    $platformCheck = true; 
    foreach ($platformCheckArray as $pca) 
    { 
     if (!$pca) 
     { 
      $platformCheck = false; 
      break; 
     } 
    } 

    $proCheck = true; 
    $cleanedPros = array(); 

    foreach ($_POST['pros'] as $pro) 
    { 
     if (!preg_match("/^[a-zA-Z0-9\-_:!' ]*$/", $pro)) 
     { 
      $proCheck = false; 
      break; 
     } 

     if (!empty($pro)) { $cleanedPros[] = $this->clean($pro); } 
    } 

    $conCheck = true; 
    $cleanedCons = array(); 

    foreach ($_POST['cons'] as $con) 
    { 
     if (!preg_match("/^[a-zA-Z0-9\-_:!' ]*$/", $con)) 
     { 
      $conCheck = false; 
      break; 
     } 

     if (!empty($con)) { $cleanedCons[] = $this->clean($con); } 
    } 

    if (!is_numeric($_POST['score'])) { $score = false; } 
    else { $score = $_POST['score']; } 

    if (empty($_POST['content'])) { $content = false; } 
    else { $content = true; } 

    // save review if all tests pass, display error otherwise 

    if ($gameTitle && $genre && $platformCheck && $proCheck && $conCheck && $score && $content) 
    { 
     $gameTitle = $gameTitle; 
     $platforms = $_POST['platforms']; 
     $reviewContent = $_POST['content']; 
     $prosText = implode(', ', $cleanedPros); 
     $consText = implode(', ', $cleanedCons); 

     $game = ORM::factory('game'); 
     $game->title = $gameTitle; 
     $game->genre_id = $genre; 
     $game->platforms = $platforms; 
     $game->save(); 

     $storedGenre = ORM::factory('genre')->where('id', $genre)->find(); 
     $storedGenre->platforms = $platforms; 
     $storedGenre->save(); 

     $review = ORM::factory('review', $id); 
     $review->content = $reviewContent; 
     $review->score = $score; 
     $review->game_id = $game->id; 
     $review->date_added = date('Y-m-d H:i:s'); 
     $review->platforms = $platforms; 
     $review->save(); 

     $pros = ORM::factory('pro'); 
     $pros->review_id = $review->id; 
     $pros->text = $prosText; 
     $pros->save(); 

     $cons = ORM::factory('con'); 
     $cons->review_id = $review->id; 
     $cons->text = $consText; 
     $cons->save(); 

     if ($game->saved && $storedGenre->saved && $review->saved && $pros->saved && $cons->saved) { $this->success('review'); } 
     else { $this->showError("Something went wrong with saving the review. Please try again."); } 
    } 
    else { $this->showError("All fields must contain values. Please try again."); } 
} 

检索复审(从评论控制器):

public function show($id, $page = 1) 
{ 
    if (is_numeric($id)) { $game = ORM::factory('game', $id); } 
    else 
    { 
     $id = ucwords(stripslashes($id)); 
     $game = ORM::factory('game')->where('title', $id)->find(); 
    } 

    if ($game->loaded) { $this->showReview($game->id, $page); } 
    else { HandiError::factory('Could not retrieve the specified review. Please check that you entered the correct value.'); } 
} 

private function showReview($id, $page = 1) 
{ 
    $page = (int)$page; 

    if ($page < 1) { $page = 1; } 

    if ($id) 
    { 
     $game = ORM::factory('game', $id); 
     $review = ORM::factory('review')->where('game_id', $game->id)->find(); 
     $genre = ORM::factory('genre')->where('id', $game->genre_id)->find(); 
     $revPlatforms = $this->db->query("SELECT * FROM platforms 
             INNER JOIN platforms_reviews AS pr ON platforms.id = pr.platform_id 
             INNER JOIN reviews ON pr.review_id = reviews.id 
             WHERE reviews.id = ?", $review->id); 
     $revPros = ORM::factory('pro')->where('review_id', $review->id)->find(); 
     $revCons = ORM::factory('con')->where('review_id', $review->id)->find(); 

     $platforms = array(); 
     foreach($revPlatforms as $rp) { $platforms[] = $rp->name; } 
     $pros = explode(', ', $revPros->text); 
     $cons = explode(', ', $revCons->text); 

     $pages = explode('&lt;split /&gt;', $review->content); 
     $count = count($pages); 

     if ($page > ($count)) { $content = $pages[0]; } 
     else { $content = $pages[$page - 1]; } 

     $view = new View('reviews/show_review'); 
     $view->content = $content; 
     $view->gameTitle = $game->title; 
     $view->genre = $genre->name; 
     $view->platforms = implode(', ', $platforms); 
     $view->pros = $pros; 
     $view->cons = $cons; 
     $view->score = $review->score; 
     $view->pages = $pages; 
     $view->render(true); 
    } 
    else { HandiError::factory('Could not retrieve the specified review. Please check that you entered the correct value.'); } 
} 

编辑2:嗯,我发现了一些关于超前时段情况:

在我的管理员索引中,我有几个查询用于根据游戏标题,平台,流派等列出评论。它基本上是一个穷人的维基。请参阅:

public function index() 
{ 
    /* show a wiki-like page with reviews listed by title, 
    * game title, genre, and platform 
    */ 

    $numGenres = $this->db->query("SELECT COUNT(id) AS num FROM genres"); 
    $numPlatforms = $this->db->query("SELECT COUNT(id) AS num FROM platforms"); 

    $genreCount = $numGenres[0]->num; 
    $platformCount = $numPlatforms[0]->num; 
    $scoreCount = 5; 

    $genreResults = array(); 
    $platformResults = array(); 
    $scoreResults = array(); 

    $gameResults = $this->db->query("SELECT LEFT(title, 1) AS letter, COUNT(id) AS count FROM games GROUP BY letter ORDER BY letter ASC"); 

    for($i = 1; $i < ($genreCount + 1); ++$i) 
    { 
     $genreResults[] = $this->db->query("SELECT genres.id AS id, genres.name AS name, COUNT(reviews.id) AS num FROM reviews 
              INNER JOIN games ON reviews.game_id = games.id 
              INNER JOIN genres ON games.genre_id = genres.id 
              WHERE genres.id = ?", $i); 
    } 

    for($j = 1; $j < ($platformCount + 1); ++$j) 
    { 
     $platformResults[] = $this->db->query("SELECT platforms.id AS id, platforms.name AS name, COUNT(reviews.id) AS num FROM reviews 
               INNER JOIN platforms_reviews AS pr ON reviews.id = pr.review_id 
               INNER JOIN platforms ON pr.platform_id = platforms.id 
               WHERE platforms.id = ?", $j); 
    } 

    for($k = 1; $k < ($scoreCount + 1); ++$k) 
    { 
     $scoreResults[] = $this->db->query("SELECT score, COUNT(id) AS num FROM reviews WHERE score = ?", $k); 
    } 

    $view = new View('reviews/index'); 
    $view->gamesByLetter = $gameResults; 
    $view->genres = $genreResults; 
    $view->platforms = $platformResults; 
    $view->scores = $scoreResults; 
    $view->render(true); 
} 

当我将这些查询的结果传递给视图时,我循环遍历它们并创建基于元类别的链接。因此,它显示了多少游戏以字母A,B等开始,并且点击其中一个链接将用户带到链接列表,每个链接都带有评论(所以,A-> Afterburner(等等)), - >对Afterburner的评论)。

当我将鼠标悬停在具有领先期的组上时,我的状态栏显示链接中缺少该时间段,即使它显示在源中。所以,即使源代码显示链接为site.com/reviews/game/。浏览器将其显示为site.com/reviews/game/这让我相信这段时间甚至没有被传递到方法中,堆栈跟踪似乎证实(它声称有一个缺失的参数,这将是期间)。

编辑3:好吧,我看看我的路线,并找不到任何东西。这就是说,我确实有一个.htaccess文件,mod_rewrite的路线看起来漂亮的SEO,所以我想知道如果这可能是问题。我自己从来没有写过mod_rewrite文件 - Kohana论坛上的人给了我这个,它工作,所以我去了。我可以理解一些涉及的regEx,但是我的regEx Fu很弱。我相信最后一行是'魔术'。

# Turn on URL rewriting 
Options +FollowSymlinks 
RewriteEngine On 

# Put your installation directory here: 
# If your URL is www.example.com/, use/
# If your URL is www.example.com/kohana/, use /kohana/ 
RewriteBase/

# Do not enable rewriting for files or directories that exist 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# For reuests that are not actual files or directories, 
# Rewrite to index.php/URL 

# Original rule: 
# RewriteRule ^(.*)$ index.php/$1 [PT,L] 

# Alternative rule: 
# RewriteRule .* index.php/$0 [PT,L] 

# 2nd alternative rule that works on ICDSoft: 
RewriteRule .* index.php?kohana_uri=$0 [PT,QSA,L] 

如果我正在阅读这个权利,那么'。'只是指任何单个字符。

Can a'。'除了表示文件扩展名或网址后缀(.com,.org等)之外,还可以在格式正确的URL中使用?我的意思是,当我将鼠标悬停在他们的链接上时,它们并未出现在Firefox的状态栏中,这导致我认为这是浏览器/格式良好问题,而不是编码问题。

+0

你能张贴您用来检索查询评价? PHP代码将会很有帮助,如果可能的话,它会在发送给MySQL之前将最终语句的回显/打印输出。 – Mike 2010-07-27 20:54:08

+0

我不使用Kohana - 是否(或可以)堆栈跟踪包括导致问题的SQL查询? – Mike 2010-07-27 21:08:31

+0

从我能看到的,没有。 – 2010-07-27 21:14:04

回答

0

时间检查所有生成的查询与探查。

in Controller :: __ construct()put

new Profiler;

并找到可能损坏的查询。

其他可能的解决方案: 走线槽你的代码,有时未关闭/未终止的数据库查询例如可以打破(或合并)的其他查询...

0

MySQL在列数据中的句点没有问题。但是,该时段用于将表名与列名分开:table.column。如果您的查询未正确转义并引用,则该时间段可能会错误地解释为表格/列分隔符。

您是如何准备查询的?

0

我认为,这个问题是在Kohana框架中,而不是在SQL中。 Chchk out从url过滤参数。试着打印你的查询,看看它在执行的时刻看起来是什么样子,并观察你的期间发生了什么。

0

编辑:对不起,我没有看到你使用Kohana版本2.x.我怀疑这适用于你。

只是一个猜测,但你有没有设置你的路线,以允许在网址期间? Kohana默认不允许句点。您需要设置路线)的第三个参数::集(到这样的事情:

Route::set('reviews', 'reviews/<name>(/<page>)', array('name' => '[^/,;?]++', 'page' => '\d+') 
    ->defaults(array(
     'controller' => 'reviews', 
     'action'  => 'load', 
     'name'  => NULL, 
     'page'  => 1, 
    )); 

查看论坛发帖http://forum.kohanaframework.org/comments.php?DiscussionID=4320