MySQLとAjaxによる星型評価ボタンの設置方法

最近よく見かけるシステムで、MySQLに対応した星型レビューボタンの設置方法です。
サンプルデモ
ダウンロード方法の英語は以下に。
Star Rater Ajax Version
興味のある方は簡易翻訳しましたので以下に。So, I found this great star rater script made in css. But I missed the web 2.0 stuff. So i played arround with it so it would work on a database without having to refresh any pages (but updating the database with AJAX). In this version i use it for rating a image (with unique id = imgId).
ただし、多少PHPの知識が必要とされますので、あらかじめご了承ください。
導入方法
(追加記事)
このシステムの脆弱性
ZAPA氏による指摘。公開から7時間で指摘エントリーを作ってくれました。
仕事はやい・・・。
MySQLとAjaxによる星型評価ボタンの脆弱性に注意
1)アイコンを手に入れる
2)JavaScriptを手に入れる
このページで以下の作業を行います。

3)PHPを作る
("localhost", “#######”, “"#######", “)は("localhost", “データベースユーザー名”, “"パスワード", “)となっています。if($_GET[’rating’] && $_GET[’imgId’]){
$dbh=mysql_connect ("localhost", “#######”, “"#######", “) or die (’I cannot connect to the database because: ‘ . mysql_error());
mysql_select_db (""#######", “);
$imgId = $_GET[’imgId’];
$text = addslashes(strip_tags($_GET[’rating’]));
//ads the rating to imgID in the database
$update = “update vote set voteNr = voteNr + 1, voteValue = voteValue + “.$_GET[’rating’].” WHERE imgId = ‘“.$imgId."‘“;
$result = mysql_query($update);
if(mysql_affected_rows() == 0){
//creates a new row if imgID has no own row yet
$insert = “insert into vote (voteNr,voteValue,imgId) values (’1’,’”.$_GET[’rating’]."’,’$imgId’)";
$result = mysql_query($insert);
}
}
mysql_select_db (""#######", “); はデータベース名を入力します。
このファイルの名前を『update.php』とする。
4)MySQLのセッティング
テーブル名を『vote』にし、フィールドを作成する。CREATE TABLE `vote` (
`voteNr` int(8) NOT NULL default ‘0’,
`voteValue` int(8) NOT NULL default ‘0’,
`imgId` varchar(100) NOT NULL default ‘’,
UNIQUE KEY `imgId` (`imgId`)
) TYPE=MyISAM;
5)CSSのダウンロード
星を表現するためのCSSをダウンロードしよう。

先ほどダウンロードしたページの5番目の項目でダウンロードできる。
6)PHPから現在の評価を取り出すコード
$rating = getCurrenRating('12');//12は例、これがIDとなって格納されているデータベースを引っ張る。
これを実行するために、以下の文をページのどこかで表示する必要がある。
<?
function getCurrenRating($imgId){
$sql= "select * from tblVote WHERE imgId= ‘“.addslashes($imgId).”’ LIMIT 0, 1”;
$result=@mysql_query($sql);
$rs=@mysql_fetch_array($result);
return @round($rs[voteValue] / $rs[voteNr],1);
}
?>
7)JavaScriptの呼び出しを忘れない
<script src="rating.js" type="text/javascript"></script>
これをヘッダーに入れておく。
『update.php』を読み込む為のJavaScriptを読み込みます。<div id="rating">
<h3>Rating:</h3>
<pre>
<ul class=’star-rating’>
<li class=’current-rating’ id=’current-rating’ style=’width: <? $ratingpx = $rating *25; echo $ratingpx;?>px’><!-- Currently <? echo $rating ?>/5 Stars.--></li>
<li><a href="javascript:rateImg(1,’<? echo $imgId ?>’)" title=’1 star out of 5’ class=’one-star’>1</a></li>
<li><a href="javascript:rateImg(2,’<? echo $imgId ?>’)" title=’2 stars out of 5’ class=’two-stars’>2</a></li>
<li><a href="javascript:rateImg(3,’<? echo $imgId ?>’)" title=’3 stars out of 5’ class=’three-stars’>3</a></li>
<li><a href="javascript:rateImg(4,’<? echo $imgId ?>’)" title=’4 stars out of 5’ class=’four-stars’>4</a></li>
<li><a href="javascript:rateImg(5,’<? echo $imgId ?>’)" title=’5 stars out of 5’ class=’five-stars’>5</a></li>
</ul>
</pre>
スタイルシートを読み込み、星型の表示を完成させます。
見事、出来上がり。
と、なるはずです。
コードも間違いが無いように使用する場合は
直接元記事からいただくようにしたほうが良いですね。
ではでは。