Topic: แจกโค้ด GuestBook (PHP)
Oldest Post First
Newest Post First
Author
Message
Admin Group
Penguin in the Sky
Joined: 17 Aug 2006
Location: GF Sector D
Online Status: Posts: 891
Reputation: 4032
Activity: 820
Next Level: 100.00%
Mileage: 36.954km
Rating: 261.72 %
Fit-Img()
Topic: แจกโค้ด GuestBook (PHP) Posted: 29 Mar 2010 at 22:29
หลังจากที่ไปช่วยทำ GuestBook ที่ Htเดียร์มี่แลนด์ ก็เลยได้ประสบการณ์ด้าน PHP มานิดหน่อย กับโค้ดที่เป็นผลงานร่วม ซึ่งกันตกลงกันว่าเอามาแจกได้ guestbook.html
<form id="form1" name="form1" method="post" action="addguestbook.php"> <td> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td width="117">Name</td> <td width="14">:</td> <td width="357"><input name="NAME" type="text" id="NAME" size="40" /></td> </tr> <tr> <td>URL</td> <td>:</td> <td><input name="URL" type="text" id="URL" size="40" /></td> </tr> <tr> <td valign="top">Comment</td> <td valign="top">:</td> <td><textarea name="MESSAGE" cols="40" rows="3" id="MESSAGE"></textarea></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td> </tr> </table> </td> </form>
addguestbook.php
<?php error_reporting(E_ALL); // Enable error reporting ini_set('display_errors', 1); // Show errors info $host=""; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name=""; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB"); $ipString=$_SERVER["HTTP_X_FORWARDED_FOR"]; $NAME = $_POST["NAME"]; $URL = $_POST["URL"]; $MSG = $_POST["MESSAGE"]; $sql="INSERT INTO $tbl_name(ID, NAME, URL, MESSAGE, DATE, IP_ADDRESS)VALUES(0, '$NAME', '$URL', '$MSG', Now(), '$ipString');"; $result=mysql_query($sql); //check if query successful if($result){ echo "Successful"; echo "<br>"; echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page } else { echo "ERROR"; } mysql_close(); ?>
viewguestbook.php
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><strong>View Guestbook | <a href="guestbook.html">Sign Guestbook</a> </strong></td> </tr> </table> <br> <?php $host=""; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name=""; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name ORDER BY ID DESC"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ ?> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td>ID</td> <td>:</td> <td><? echo $rows['ID']; ?></td> </tr> <tr> <td width="117">Name</td> <td width="14">:</td> <td width="357"><? echo $rows['NAME']; ?></td> </tr> <tr> <td>URL</td> <td>:</td> <td><? echo $rows['URL']; ?></td> </tr> <tr> <td valign="top">Comment</td> <td valign="top">:</td> <td><? echo $rows['MESSAGE']; ?></td> </tr> <tr> <td valign="top">Date/Time </td> <td valign="top">:</td> <td><? echo $rows['DATE']; ?></td> </tr> </table></td> </tr> </table> <br> <? } mysql_close(); //close database ?>
หมายเหตุ หน้า guestbook.html กับ viewguestbook.php สามารถจับรวมกันได้ตามความเหมาะสม Features - รันเลข Entry ID อัตโนมัติ - เก็บหมายเลข IP Address ของผู้ที่เจิมสมุดเยี่ยมไว้ในฐานข้อมูล - ลงเวลาการบันทึกข้อความตามเวลาของ Database server - มีขนาดเล็ก - ติดตั้งง่าย - ต่อเติมได้ง่าย Limitations - โค้ดข้างบนจะลงเวลาตามเวลาของ Database server เท่านั้น - หมายเลข IP Address ที่เก็บไว้ต้องเปิดดูจากฐานข้อมูลเท่านั้น - ไม่มีระบบ admin ด้านหน้าเว็บ Requirements - Web Server with PHP - MySQL database ส่วนใน table ของ database ต้องมี fields ดังนี้ ID เป็น INT(4) สถานะ Auto Increment และ Primary Key NAME เป็น VARCHAR(60) URL เป็น VARCHAR(255) MESSAGE เป็น TEXT DATE เป็น TIMESTAMP IP_ADDRESS เป็น VARCHAR(255) หมายเหตุ ตรงที่เป็น VARCHAR ทั้งหมดสามารถใ้ช้ TEXT แทนได้เพื่อความง่าย และให้ตั้ง collation เป็น utf8_general_ci เพื่อให้สามารถย้ายข้อมูลไปวางที่อื่นได้ง่าย
IP Logged
Forum Jump: -- Select Forum --
Announcements
  - For A-Zone
  - For C-ES Field
  - Private Announcements
Free Talk
Multimedia Center
  - MAD
  - Sensitive Contents
Events
Anime Talk
Manga Talk
Doujinshi Talk
Vocaloid Room
Computer & Internet
  - Web Room
  - Server Operation Room
PC Games
Console Games
GRAND FORCE Battle Card
  - GFBC Dev Blog
Lunentia ZERO Reborn
  - [ZR]Private-Events
  - Guild Talk
GF Project
  - GF Server
  - Story
Report Bugs and Whatever
Empty Room
GF Archive
Lunentia/ZERO Archives
  - ZERO-ONE Server
  - MF Server
  - Profiles / Game Data
  - LZ-Files
  - Lunentia ZERO Game Support
  - Test Area
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum
GRAND FORCE Project Community This page was generated in 0.047 seconds.
Hit Counter
Hit Counter
Network Information
Network Information
GRAND FORCE PROJECT Community
http://hq.gfcom.info/
IP=10.0.0.227 for 10.1.76.1
Miku Clock
Miku Clock
Miku is here. Click to wake!
GF Alliance
Useful links
Stats