$Conn = Mysql Connect($Host,$User,$Pass)Or Die( ติดต่อ Host ไม่ได้br> );
1
dbcon.php
<?php
$host = "127.0.0.1";
$user = "root";
$pass = "123456";
$dbname = "dbregister";
$tbuser = "tbuser";
$conn = mysql_connect($host,$user,$pass)or die("ติดต่อ Host ไม่ได้br>");
$db =mysql_select_db($dbname) or die ("ติดต่อฐานข้อมูลไม่ได้ ");
?>
FormRegister.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
title>สมัครสมาชิก</title>
</head>
body
<form id="form1" name="form1" method="post" action="AddData.php">
<table width="60%" border="0">
<tr
<td width="90">ชื่อล็อกอิน</td>
<td width="235"<input type="text" name="logname" id="logname" /</td>
<td width="102">เพศ</td>
<td width="206"<p>
<label
<input type="radio" name="gender" value="male" id="gender_0" />
ชาย</label>
<input type="radio" name="gender" value="female" id="gender_1" />
<label>หญิง</label>
<br />
</p</td>
</tr
<tr
<td>รหัสผ่าน</td>
<td<input type="password" name="pw" id="pw" /</td>
<td>อายุ</td>
<td<select name="age" id="age">
<option value="9-18">9-18 ปี</option>
<option value="19-25">19-25 ปี</option>
<option value="26-45">26-45 ปี</option>
<option value="46-60">46-60 ปี</option>
<option value="61-80">61-80 ปี</option>
</select</td>
</tr
<tr
<td>ชื่อ-นามสกุล </td>
<td<input type="text" name="name" id="name" /</td>
<td>งานอดิเรก</td>
<td<input name="hobby1" type="checkbox" id="hobby1" value="sport" />
เล่นกีฬา br />
<input name="hobby2" type="checkbox" id="hobby2" value="reading" />
อ่านหนังสือ br />
<input name="hobby3" type="checkbox" id="hobby3" value="tour" />
ท่องเที่ยว br />
<input name="hobby4" type="checkbox" id="hobby4" value="game" />
เกม </td>
</tr
<tr
<td>ที่อยู่</td>
<tdtextarea name="address" id="address" cols="30" rows="5"</textarea</td>
<td>รู้จักเว็บไซต์นี้ จากแหล่งใด</td>
<td<select name="ads" id="ads">
<option value="newspaper">หนังสือพิมพ์</option>
<option value="book">หนังสือในเครือสำนักพิมพ์</option>
<option value="internet">อินเตอร์เน็ต</option>
<option value="poster" selected="selected">ใบปลิว</option>
</select</td>
</tr
<tr
<td>อีเมล์</td>
<td<input name="email" type="text" id="email" size="25" /</td>
<td</td>
<tdnbsp;</td>
</tr
<tr
<tdnbsp;</td>
<td<input type="submit" name="Submit" id="Submit" value="ส่งข้อมูล" /</td>
<td<input type="reset" name="Reset" id="Reset" value="ล้างข้อมูล" /</td>
<tdnbsp;</td>
</tr
</table>
</form>
</body>
</html>
สร้างฐานข้อมูล
AddData.php
<?php
include("dbcon.php");
$sql = "insert into `$tbuser` ( `logname` , `pw` , `name` , `address` , `age` , `gender` , `ads`, `hobby1` , `hobby2` , `hobby3` , `hobby4` , `email` )
values ('$logname', '$pw', '$name', '$address', '$age', '$gender', '$ads', '$hobby1', '$hobby2', '$hobby3', '$hobby4', '$email' )";
mysql_query("set NAMES tis620");
$sqlquery=mysql_db_query($dbname, $sql);
$checkadd = "select * from `$tbuser` where logname='$logname' ";
$result = mysql_db_query($dbname, $checkadd);
$num=mysql_num_rows($result);
if ($num==1)
{
print"<brเพิ่มข้อมูลผู้ใช้ลงในฐานข้อมูลเรียบร้อยแล้ว ";
}elseif($num>=1)
{
print"<brชื่อผู้ใช้งานซ้ำ กรุณาใช้ชื่ออื่น ";
}else
{
print"ระบบขัดข้อง หรือ มีข้อผิดพลาด !!!";
};
mysql_close($conn);
?>
fetcharray.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
title>รายชื่อสมาชิก</title>
</head>
<?php
include("dbcon.php");
$sql = "select *from $tbuser ";
mysql_query("set NAMES tis620");
$sqlquery=mysql_db_query($dbname, $sql);
print "<h3>จำนวนผู้สมัครสมาชิกมีทั้งหมด ".mysql_num_rows($sqlquery)." คน ซึ่งมีข้อมูลดังนี้</h3>";
print "<table>";
print "<tr bgcolor=0066FF>";
print "<td align = center>ผู้ใช้</td>";
print "<td align = center>รหัสผ่าน</td>";
print "<td align = center>อีเมล</td>";
print "<td align = center>ชื่อ-นามสกุล</td>";
print "<td align = center>ที่อยู่</td>";
print "<td align = center>อายุ</td>";
print "<td align = center>เพศ</td>";
print "<td align = center>แหล่งที่มา</td>";
print "<td align = center>งานอดิเรก</td>";
print "</tr>";
while ($array = mysql_fetch_array($sqlquery))
{
$logname = $array['logname'];
print "<tr bgcolor=00CCFF>";
print "<td>".$array['logname'] ."</td>";
print "<td>".$array['pw'] ." </td>";
print "<td>".$array['email'] ." </td>";
print "<td>".$array['name'] ." </td>";
print "<td>".$array['address'] ." </td>";
print "<td>".$array['age']." </td>";
print "<td>".$array['gender']." </td>";
print "<td>".$array['ads']." </td>";
print "<td>".$array['hobby'] ." ".$array['hobby2'] ." ".$array['hobby3']." ". $array['hobby4'] ."</td>";
print "</tr>";
}
print"</table>";
mysql_close($conn);
?>
</html>