¼«Ê¬¤Î¥µ¥¤¥È¤Ë¥¢¥ó¥±¡¼¥È¤òÀßÃÖ¤·¤Æ¤ß¤Þ¤·¤ç¤¦.º£²ó¤ÏDB¤ò»È¤Ã¤Æºî¤Ã¤Æ¤ß¤Þ¤¹¡¥
°Ê²¼¤Î¥Æ¡¼¥Ö¥ë¤òÍÑ°Õ¤·¤Æ¤¯¤À¤µ¤¤¡¥DB̾¡¤¥Æ¡¼¥Ö¥ë̾¤ÏǤ°Õ¤Ç¤«¤Þ¤¤¤Þ¤»¤ó¡¥
(¤â¤Á¤í¤ó¥Õ¥£¡¼¥ë¥É̾¤â¤ª¹¥¤¤Ê¤â¤Î¤ò¡¥counter01Åù¤Ç¤Ê¤¯¤ÆÂç¾æÉפǤ¹)
¥Õ¥£¡¼¥ë¥É¤Î¿ô¤¬¥æ¡¼¥¶¤¬ÁªÂò¤¹¤ë¥é¥¸¥ª¥Ü¥¿¥ó¤Î¿ô¤Ë¤Ê¤ê¤Þ¤¹¡¥
¥Õ¥£¡¼¥ë¥É̾
|
¥Õ¥£¡¼¥ë¥É·¿
|
°|
counter01
|
int |
|
counter02
|
int |
|
counter03
|
int |
|
½ÐÍè¾å¤¬¤Ã¤¿table¤Î³ÆÍ×ÁǤˤϡ¢½é´üÃͤÎ0¤òÆþ¤ì¤Æ¤ª¤¤Þ¤·¤ç¤¦¡£
mysql> insert into
¥Æ¡¼¥Ö¥ë̾ set counter01="0", counter02="0", counter03="0";
¥¢¥ó¥±¡¼¥È¤Ë»ÈÍѤ¹¤ë¶õ¤Î¥Õ¥¡¥¤¥ëanswer.txt¤òºîÀ®¤·¤Æ¤¯¤À¤µ¤¤¡£(touch¥³¥Þ¥ó¥É)
¤Þ¤¿¡¤¥°¥é¥Õ¤òÉÁ¤¯¤¿¤á¤Î¥¤¥á¡¼¥¸
graph.gif¤òºîÀ®¤·¤Æ¤¯¤À¤µ¤¤¡¥
¤½¤·¤Æ¥¢¥ó¥±¡¼¥ÈÆþÎÏ¥Õ¥©¡¼¥à¤Ç¤¢¤ë
ank.html¤ò¡¤°Ê²¼¤ÎÆâÍƤǺîÀ®¤·¤Æ¤¯¤À¤µ¤¤¡¥
<html>
<body>
<b>¥¢¥ó¥±¡¼¥È PHP Dream¤Ë¤Ä¤¤¤Æ</b>
<form method = "post" action = "ank.php">
<input type = "radio" name = "type" value = "1" checked>Îɤ«¤Ã¤¿<br>
<input type = "radio" name = "type" value = "2" >°¤«¤Ã¤¿<br><br>
<input type = "radio" name = "type" value = "3" >¤½¤Î¾
<input type = "text", name = "test_about_other" maxlength="100">
<input type ="submit" value="Åê¹Æ¤¹¤ë"><br>
</form>
</body>
</html>
|
¥æ¡¼¥¶¤Ë¼¨¤¹¤¿¤á¤Î¥¢¥ó¥±¡¼¥ÈÆþÎÏ·ë²Ì
ank.php¤ò¡¤°Ê²¼¤ÎÆâÍƤǺîÀ®¤·¤Æ¤¯¤À¤µ¤¤¡¥
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP"></head>
<body>
<?php
$posttype = $_POST['type'];//¥é¥¸¥ª¥Ü¥¿¥ó¤Î¥¿¥¤¥×¤òÆþ¤ì¤ë
//Ãí°Õ¡
$num = 3;//¥é¥¸¥ª¥Ü¥¿¥ó¤Î¿ô
$strLength = 100;//ʸ»úÎó¤ÎºÇÂç¤ÎŤµ
$filename = "answer.txt";//¥¢¥ó¥±¡¼¥È¤Îµ½Ò¹àÌܤòÊݸ¤¹¤ëtxt¥Õ¥¡¥¤¥ë̾
//DB //¥Ç¡¼¥¿¥Ù¡¼¥¹¤ËÀܳ¤¹¤ë¡¥°ú¿ô¤ËÃí°Õ
mysql_connect("127.0.0.1","¥æ¡¼¥¶Ì¾", "¥Ñ¥¹¥ï¡¼¥É");
mysql_select_db("¥Ç¡¼¥¿¥Ù¡¼¥¹Ì¾");//¤¢¤Ê¤¿¤Î¥Ç¡¼¥¿¥Ù¡¼¥¹Ì¾¤òÆþ¤ì¤Æ²¼¤µ¤¤
//¥Æ¡¼¥Ö¥ë¤«¤é¾ðÊó¤òÆÀ¤ë ¥Æ¡¼¥Ö¥ë̾¤Ï³Æ¼«Êѹ¹¤·¤Æ²¼¤µ¤¤
$result = mysql_query("select * from ¥Æ¡¼¥Ö¥ë̾");
$array = mysql_fetch_array($result);
print "¥¢¥ó¥±¡¼¥È¤Ë¶¨ÎϤ·¤Æ¤¤¤¿¤À¤¤¢¤ê¤¬¤È¤¦¤´¤¶¤¤¤Þ¤¹¡£<br>";
print "¤¢¤Ê¤¿¤Î²òÅú¡§";
//Ãí°Õ¢
if( $posttype == 1 ){
print "Îɤ«¤Ã¤¿<br><br>";
$counter = $array['counter01'];
}
else if( $posttype == 2 ) {
print "°¤«¤Ã¤¿<br><br>";
$counter = $array['counter02'];
}
else if( $posttype == 3 ) {
$counter = $array['counter03'];
if(mb_strlen($_POST['test_about_other']) > $strLength) {
//ºÇÂç¤Îʸ»ú¿ô
$limitedStr = mb_substr($_POST['test_about_other'], 0,
$strLength);
echo "over100";
}
else $limitedStr = $_POST['test_about_other'];
print "¤½¤Î¾ \"".$limitedStr."\"<br><br>";
$file = fopen( $filename, "r+");
flock( $file, LOCK_EX ); //Ãí°Õ¢-£²
fseek($file, 0 , SEEK_END);
//»þ´Ö¤òÆþ¤ì¤ë
$today = getdate();
fwrite($file, $today[year]."ǯ".$today[mon]."·î".$today[mday]."Æü" .$today[hours]."»þ".$today[minutes]."ʬ".$today[seconds]."ÉÃ");
fwrite($file, ":¥³¥á¥ó¥È:\"".$limitedStr."\"\n");
flock($file, LOCK_UN );
fclose($file);
//»þ´Ö¤òÆþ¤ì¤ëµ½Ò¤Ï¤³¤³¤Þ¤Ç
}
//¥é¥¸¥ª¥Ü¥¿¥ó¤ÎÃͤ¬¤ª¤«¤·¤¤¤È¤
else {
echo "ERROR";
exit;
}
$counter++;//Áª¤Ð¤ì¤¿¥é¥¸¥ª¥Ü¥¿¥ó¤Îɼ¤Î¿ô¤ò1Áý¤ä¤¹
//Ãí°Õ£
//DB¤Î¹¹¿·
$posttype=addslashes($posttype);
$result = mysql_query("update ¥Æ¡¼¥Ö¥ë̾ set counter0".$posttype."=$counter");
//½ÉÂê¡
//php.ini¤¬°Ê²¼¤ÎÀßÄê¤Ê¤é$posttype($_POST¤è¤ê°ú¤·Ñ¤¤¤ÀÊÑ¿ô)¤òaddslashes´Ø¿ô¤Ë
//Ä̤¹É¬ÍפϤ¢¤ê¤Þ¤»¤ó.
//php.ini
//; Magic quotes
//;
//
//; Magic quotes for incoming GET/POST/Cookie data.
//magic_quotes_gpc = On
//off¤Ê¤é
//¥·¥ó¥°¥ë¥¯¥©¡¼¥È¡¢¥À¥Ö¥ë¥¯¥©¡¼¥È¡¢¥Ð¥Ã¥¯¥¹¥é¥Ã¥·¥å¡¢
//NULL¥Ð¥¤¥È¤ò¥Ð¥Ã¥¯¥¹¥é¥Ã¥·¥å¤Çʸ»ú¤ËÊÑ´¹
//addslashes();
//¹¹¿·¸å¤Î·ë²Ì¤ò¼èÆÀ
$result = mysql_query("select * from ¥Æ¡¼¥Ö¥ë̾");
$array = mysql_fetch_array($result);
$sum = 0;//Á´ÂΤÎɼ¤ò¿ô¤¨¤ë¤¿¤á¤Ë»ÈÍѤµ¤ì¤ë$sumÊÑ¿ô
//Á´ÂΤÎɼ¤ò·×»» ½ÉÂê¢
for( $i = 0; $i<$num; $i++){
$sum += $array[$i];
}
//¥°¥é¥Õ¤ÎÉÁ²è
//graph¤ÎŤµ¤ò³ä¹ç¤Ç·×»»¤¹¤ë¤¿¤á¤Ë»ÈÍѤ¹¤ëÊÑ¿ô
$graph_length = 0;
for($i = 0; $i <$num; $i++){
//¾®¿ôÅÀ°Ê²¼¤òŤ¯¤·¤Ê¤¤¤è¤¦¤Ë¹©É× Ãí°Õ¤
$percentage = round(($array[$i]*100/$sum)*10)/10;
$graph_length = (int)($percentage*2);//¾®¿ôÅÀ¤ò¥«¥Ã¥È
$button_num = $i+1;//¥é¥¸¥ª¥Ü¥¿¥ó¤ÎÈÖ¹æ¤ò·×»»¤¹¤ë
//¥°¥é¥Õ¤ÎÉÁ²è
print "¥Ü¥¿¥ó$button_num <img src=\"../img/graph.gif\" width=\"$graph_length \" height=\"5\"> $percentage"."% ";
print $array[$i]."ɼ<br><br>";
}
//ºÇ¸å¤ËÌá¤ë¥Ü¥¿¥ó
print "<a href = \"./ank.html\">Ìá¤ë</a>";
?>
</body>
</html>
|
¥æ¡¼¥¶¤¬²òÅú¤·¤¿¥Æ¥¥¹¥È¥Õ¥©¡¼¥à¤ÎÆâÍÆ(answer.txt)¤Ï°Ê²¼(answer.php)¤Ç¸«¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡¥¡ÊÌÞÏÀ¥Æ¥¥¹¥È¥Õ¥¡¥¤¥ë¤ò¤½¤Î¤Þ¤Þ¸«¤ë¤³¤È¤â½ÐÍè¤Þ¤¹¡Ë
<?php
$fp = fopen( "answer.txt", "r" );
while( !feof( $fp ) ){
//feof´Ø¿ô ¥Õ¥¡¥¤¥ë¥Ý¥¤¥ó¥¿¤¬EOF(END OF FILE)¤ËÅþ㤷¤¿¤étrue
print ( fgets( $fp )."<br>" );//1k¤Þ¤Ç maxlength
}
?>
|
»î¤·¤Æ¤ß¤ë(ank.html¤Èank.php)
»î¤·¤Æ¤ß¤ë(answer.php)
¢£²òÀâ--------
ank.html¤Ç¤Ï¥é¥¸¥ª¥Ü¥¿¥ó¤Î¥Õ¥©¡¼¥à¤òºîÀ®¤·¤Æ¤¤¤Þ¤¹¡¥value°À¤ÎÃͤDz¿¤¬Áª¤Ð¤ì¤¿¤«ank.php¤ÏÍý²ò¤¹¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡¥
textbox¤ÏºÇÂç100ʸ»ú¤Þ¤Ç¤ÎÀ©¸Â¤òÀߤ±¤Æ¤¤¤Þ¤¹¡¥(maxlength="100")
ank.php¤Ç¤¹¤¬¡¤¤³¤Î¥×¥í¥°¥é¥à¤Ç¤Ï°Ê²¼¤Î¤è¤¦¤Êή¤ì¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡¥
¡DB¤Ë¥¢¥¯¥»¥¹¡¥º£DB¤ËÆþ¤Ã¤Æ¤¤¤ë¥¢¥ó¥±¡¼¥È·ë²Ì¤ò¼èÆÀ¤¹¤ë¡¥¡ÊÃí°Õ¢¤ÎÁ°¤Þ¤Ç¡Ë
¢¥æ¡¼¥¶¤¬²¡¤·¤¿¥é¥¸¥ª¤ÎÆÃÄꤷ¤Æ¡¤¥æ¡¼¥¶¤¬Áàºî¤·¤¿ÆâÍƤòɽ¼¨¡ÊÃí°Õ¢¡Ë
£²¡¤µ¤ì¤¿¥Ü¥¿¥ó¤Î¥Õ¥£¡¼¥ë¥É¤ò£±Áý¤ä¤¹
¡Ê¤³¤Î¤È¤¤Ë¥é¥¸¥ª¥Ü¥¿¥ó¤¬3ÈÖÌܤÀ¤Ã¤¿¤é¥æ¡¼¥¶¤¬ÆþÎϤ·¤¿¥Æ¥¥¹¥È¤òanswer.txt¤ËÁÞÆþ¡Ë ¡Ê¤³¤³¤Þ¤Ç¤ÇÃí°Õ£¤ÎÁ°¡Ë
¤DB¤Ë¥Õ¥£¡¼¥ë¥É¤ÎÊѹ¹¤òÈ¿±Ç¤µ¤»(Ãí°Õ£)¡¤É¼¤Î¹ç·×¿ô¤ò·×»»¡¤¥°¥é¥Õ¤È¤·¤Æɽ¼¨¤¹¤ë
answer.php¤Ç¤Ïñ½ã¤Ëanswer.txt¤Ë½ñ¤¹þ¤Þ¤ì¤¿ÆâÍƤòɽ¼¨¤·¤Æ¤¤¤Þ¤¹¡¥
----
Ãí°Õ¡¤ÏÀÅŪ¤ÊÊÑ¿ô¤Ë¤·¤¿¤Û¤¦¤¬¤¤¤¤¤«¤â¤·¤ì¤Þ¤»¤ó¡¥¤³¤ì¤¬¤É¤¦¤¤¤¦¤³¤È¤«À§ÈóÄ´¤Ù¤Æ¤¯¤À¤µ¤¤¡¥
Ãí°Õ¢¤Ç¤ÏŤ¤¤Ç¤¹¤¬¡¤¥é¥¸¥ª¥Ü¥¿¥ó¤Ë½¾¤Ã¤¿¥×¥í¥°¥é¥à¤ÎÆ°ºî¤ò½ñ¤¤¤Æ¤¤¤Þ¤¹¡¥
$array = mysql_fetch_array($result);¤ÇÆÀ¤é¤ì¤¿$array¤«¤é¡¤DB¤Î¥Õ¥£¡¼¥ë¥É̾¤ò»ØÄꤹ¤ë¤³¤È¤Ç¡¤¥ì¥³¡¼¥É¤Ë¥¢¥¯¥»¥¹¤¹¤ë¤³¤È¤¬²Äǽ¤Ç¤¹¡¥
Ãí°Õ¢-£²¤Ç¤Ï¤¤¤Þ¤Þ¤Çflock´Ø¿ô¤ÎÂ裲°ú¿ô¤Ï¿ô»ú¤Ç¤·¤¿¤¬¡¤Ê¸»ú¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡¥
¤³¤ì¤Ï¥Þ¥¸¥Ã¥¯¥Ê¥ó¥Ð¡¼¤È¤¤¤¤¡¤°ìÄê¤ÎÃͤǤ¹¡¥
°ú¿ô¤ÎÃͤ¬¡¤2¤ä3¤Ç¤Ï²¿¤ò¤ä¤Ã¤Æ¤¤¤ë¤«¤ï¤«¤é¤Ê¤¤¤Î¤Ç¡¤ÊÑ¿ô¤Î̾Á°¤ÇÇÓ¾À©¸æ¤ò¤·¤Æ¤¤¤ëÅù¤òÌÀ¤é¤«¤Ë¤·¤Æ¤¤¤Þ¤¹¡¥
Ãí°Õ£$_POST¤ä$_GET¤Ë³ÊǼ¤µ¤ì¤Æ¤¤¤¿¡¢³°Éô¤«¤é¼è¤ê¹þ¤àÊÑ¿ô¤Ë¤Ä¤¤¤Æ¤Ï¡¢DB¤ÎSQLʸ¤ËÅêÆþ¤¹¤ëÁ°¤Ë¡¢addslashes()¤ÇÆüìʸ»ú(¥·¥ó¥°¥ë¥¯¥©¡¼¥È¡¢¥À¥Ö¥ë¥¯¥©¡¼¥È¡¢¥Ð¥Ã¥¯¥¹¥é¥Ã¥·¥å¡¢NULL¥Ð¥¤¥È)¤ò¥¨¥¹¥±¡¼¥×¤·¤Þ¤·¤ç¤¦¡£php.ini¤ÎÀßÄê¤Ç¡¤¥æ¡¼¥¶¤ÎÆþÎϤË'¤ä"Åù¤¬¤¢¤Ã¤Æ¤â¼«Æ°Åª¤Ë¥¨¥¹¥±¡¼¥×¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡¥
ex.'¡¡¢ª \' ¤ò¼«Æ°Åª¤ËÊÑ´¹
»þ´Ö¤Î¤¢¤ëÊý¤Ï¡¢½ÉÂꡤò¤ä¤Ã¤Æ¤ß¤Þ¤·¤ç¤¦¡ª
Ãí°Õ¤¤Ç¤Ï¤½¤Î¤Þ¤Þɽ¼¨¤¹¤ë¤È¾®¿ôÅÀ¤¬Ä¹¤¯¤Ê¤Ã¤Æ¤·¤Þ¤¦¤Î¤Ç¾®¿ôÅÀ¤ò¥«¥Ã¥È¤·¤Æ¤¤¤Þ¤¹¡¥
----
º£²ó¤Î½ÉÂê¤Ç¤¹¤¬¡¤
½ÉÂê¡php.ini¤Îmagic_quotes_gpc¤ÎÀßÄê¤òÄ´¤Ù¤Æ¤ß¤Þ¤·¤ç¤¦¡¥¥Ò¥ó¥È¤Ï¥³¥á¥ó¥È¤È¤·¤Æ¥×¥í¥°¥é¥à¤Ë»Ä¤·¤Æ¤ª¤¤Þ¤·¤¿¡¨¡Ë
¤Þ¤¿¡¢magic_quotes_gpc¤ÎOn¤ÈOff¤òÀÚ¤êÂؤ¨¤Æ¤ß¤Æ¡¢µóÆ°¤¬¤É¤¦ÊѤï¤ë¤«»î¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
½ÉÂê¢Á´ÂΤÎɼ¤ò¤¤¤Á¤¤¤Á·×»»¤¹¤ë¤Î¤Ï¼ê´Ö¤Ç¤¹¡¥DB¤Ë³ÊǼ¤·¤¿¥×¥í¥°¥é¥à¤ò½ñ¤¤Þ¤·¤ç¤¦¡ª
¥Ò¥ó¥È¡§°Ê²¼¤Î¤è¤¦¤Ê¥Æ¡¼¥Ö¥ë¤òºî¤Ã¤Æ¤ß¤Æ¤Ï¡©
¥Õ¥£¡¼¥ë¥É̾
|
¥Õ¥£¡¼¥ë¥É·¿
|
°|
sum |
int |
|
counter01
|
int |
|
counter02
|
int |
|
counter03
|
int |
|
Æñ¤·¤¯¤Ê¤Ã¤Æ¤¤Þ¤·¤¿¤Í¡¥¤Ç¤¹¤¬¡¤ÃÏÆ»¤Ë¼«Ê¬¤ÇÄ´¤Ù¤ë¤³¤È¤¬php¥Þ¥¹¥¿¡¼¤ÎÆ»¤Ç¤¹¡¥
php¤Ë²¦Æ»¤Ê¤·¡ª¤Ê¤ó¤Æ¡¦¡¦¡¦¡Ê¾Ð¡Ë
¤¬¤ó¤Ð¤Ã¤Æ¤¤¤¤Þ¤·¤ç¤¦¡°¡°