หน้าต่างหน้าหลัก
<?php
session_start();
$con = mysql_connect("localhost","root","1234");
$conn = mysql_select_db("mystore",$con);
mysql_query("SET NAMES UTF8");
$sql = "select * from customer where Customer_id = '".$_SESSION['User_id']."' ";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//การเชื่อมต่อฐานข้อมูล
?>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<link rel="stylesheet" href="style1.css">
</head>
<body>
<br/><br/><h3 style="color:blue" align ="right" ><?php echo $row['Customer_Name']." ".$row['Customer_Lastname']." "; ?>
<a href='logout.php'><u>ออกจากระบบ</u></a></h3>
<br/>
<center><h3 style="color:blue"><u>ข้อมูลลูกค้า</u></h3></center>
<center>
<table style="width:60%">
<tr >
<td><h4 style="color:red" align ="right"><u><a href='add_customer.php?id=$a'>เพิ่มข้อมูลลูกค้า</u></h4></a></td>
</tr>
</table>
</center>
<center>
<table style="width:60%" id="them1">
<tr>
<th> ID</th>
<th> ชื่อ-นามสกุล</th>
<th> จังหวัด</th>
<th> เบอร์โทรศัพท์</th>
<th> แก้ไข</th>
<th> ลบ</th>
</tr>
<?php
//การแสดงฐานข้อมูล
$sql = "Select * from customer";
$query = mysql_query($sql);
while( ($row = mysql_fetch_array($query)) ) {
echo "<tr align='center'>
<td>".$row['Customer_id']."</td>
<td>".$row['Customer_Name']." ".$row['Customer_Lastname']."</td>
<td>".$row['Province']."</td>
<td>".$row['Telephone']."</td>
<td><a href='edit_customer.php?id=$row[Customer_id]'>คลิก</a></td>
<td><a href='del_customer.php?id=$row[Customer_id]'>คลิก</a></td>
</tr>";
}
?>
</table>
</center>
<hr>
</body>
</html>
หน้าต่างการ ADD ข้อมูลเข้าฐานข้อมูล
<?php
$con = mysql_connect("localhost","root","1234");
$conn = mysql_select_db("mystore",$con);
mysql_query("SET NAMES UTF8");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<link rel="stylesheet" href="style1.css">
<script langudate="javascript">//ส่วนการเขียนjavascript
//ฟังก์ชั่นตรวจสอบเงื่อนไข
function chName(text) {
TextName = new RegExp(/\w{3,}/);
if (!TextName.test(text)) {
alert("ชื่อควรมีความยาวไม่ต่ำกว่า 3 ตัวอักษร และ เป็นภาษาอังกฤษ");
document.userForm.name.focus();
return false;
}
}
function chnumtel1(num){
CheckNum = new RegExp( /^\d{1,}$/);
if (!CheckNum.test(num)) {
alert("กรุณากรอกเฉพาะตัวเลข 0-9 เท่านั้น");
document.userForm.telephone.value = "";
document.userForm.telephone.focus();
}
}
function chnumtel2(num) {
NumTelephone = new RegExp( /^\d{10,}$/);
if (!NumTelephone.test(num)) {
alert("เบอร์โทรศัพท์ควรมีตัวเลขไม่ต่ำกว่า 10 ตัว");
document.userForm.telephone.focus();
return false;
}
}
function chUsername(text) {
TextUsername = new RegExp(/\w{3,}/);
if (!TextUsername.test(text)) {
alert("Username มีความยาวไม่ต่ำกว่า 5 ตัวอักษร ");
document.userForm.username.focus();
return false;
}
}
function chPassword(text) {
TextPassword = new RegExp(/\w{8,}/);
if (!TextPassword.test(text)) {
alert("Password มีความยาวไม่ต่ำกว่า 8 ตัวอักษร ");
document.userForm.password.focus();
return false;
}
}
function goback(){
window.location = 'Lab5_55523206055-1.php';
}
function fncSubmit(){
if(document.form1.name1.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.name1.focus();
return false;
}
if(document.form1.lastname.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.lastname.focus();
return false;
}
if(document.form1.Gender.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.lastname.focus();
return false;
}
if(document.form1.date.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.date.focus();
return false;
}
if(document.form1.address.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.address.focus();
return false;
}
if(document.form1.province.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.province.focus();
return false;
}
if(document.form1.zipcode.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.zipcode.focus();
return false;
}
if(document.form1.telephone.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.telephone.focus();
return false;
}
if(document.form1.Description.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.Description.focus();
return false;
}
if(document.form1.username.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.username.focus();
return false;
}
if(document.form1.password.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.password.focus();
return false;
}
if(document.form1.Repassword.value == ""){
alert('กรุณากรอกข้อมูลให้ครบถ้วน!!');
document.form1.Repassword.focus();
return false;
}
if(document.form1.password.value !== document.form1.Repassword.value){
alert('password ไม่ตรงกัน');
document.form1.password.focus();
return false;
}
document.form1.submit();
}
</script>
</head>
<body>
<?php
//การ add ข้อมูลเข้าฐ้านข้อมูล
if(isset($_POST['submit1'])){
$Customer_name = $_POST['name1'];
$Customer_Lastname = $_POST['lastname'];
$Gender = $_POST['Gender'];
$year_now = date("Y");
$year_birth = substr($_POST['date'],0,4);
$idge = $year_now - $year_birth;
$Birthdate = $_POST['date'];
$Address = $_POST['address'];
$Province = $_POST['province'];
$Zipcode = $_POST['zipcode'];
$Telephone = $_POST['telephone'];
$Customer_Description = $_POST['Description'];
$username = $_POST['username'];
$password = $_POST['password'];
mysql_select_db("mystore",$con) or die('Select error!!');
$sql = "insert into customer(Customer_Name,Customer_Lastname,Gender,Age,Birthdate,Address,Province,Zipcode,Telephone,Customer_Description,username,password) VALUES
('$Customer_name','$Customer_Lastname','$Gender','$idge','$Birthdate','$Address','$Province','$Zipcode','$Telephone','$Customer_Description','$username','$password')";
if(mysql_query($sql)){
echo "<script>window.location = 'Lab5_55523206055-1.php'</script>";
}
}
?>
// form ในการรับค่าของข้อมูล
<form id="addForm" method='post' action="" name="form1" onSubmit="JavaScript:return fncSubmit();">
<center>
<table border='0' style="width:50%">
<tr><td>
<h3 style="color:red"><u>เพิ่มข้อมูลลูกค้า</u></h3>
<fieldset style="color:black" width="300px"height="40%">
<legend> ข้อมูลลูกค้า </legend>
<table border='0' style="width:100%">
<tr>
<td>ชื่อ </td>
<td> : <input name="name1" type="text" size="20" OnBlur="JavaScript:return chName(this.value);"></td>
</tr>
<tr>
<td>นามสกุล </td>
<td> : <input name="lastname"type="text" size="20" ></td>
</tr>
<tr>
<td>เพศ </td>
<td> : <input type="radio" name="Gender" value="ชาย">ชาย<input type="radio" name="Gender" value="หญิง">หญิง</a></td>
</tr>
<tr>
<td>วัน-เดือน-ปี-เกิด </td>
<td> : <input class=other type="date" name="date"></td>
</tr>
<tr>
<td>ที่อยู่ </td>
<td> : <input name="address" type="text" size="40"></td>
</tr>
<tr>
<td>จังหวัด </td>
<td> : <select name="province" >
<option value=""> ---เลือก---</option>
<option value="เชียงราย">เชียงราย</option>
<option value="เชียงใหม่">เชียงใหม่</option>
<option value="น่าน">น่าน</option>
<option value="พะเยา">พะเยา</option>
<option value="แพร่">แพร่</option>
<option value="แม่ฮ่องสอน">แม่ฮ่องสอน</option>
<option value="ลำปาง">ลำปาง</option>
<option value="ลำพูน">ลำพูน</option>
<option value="อุตรดิตถ์">อุตรดิตถ์</option>
</select></td>
</tr>
<tr>
<td>รหัสไปรษณีย์ </td>
<td> : <input name="zipcode" type="text" size="5" ></td>
</tr>
<tr>
<td>เบอร์โทรศัพท์ </td>
<td> : <input name="telephone" type="text" size="20" onKeyUp="JavaScript:chnumtel1(this.value);" OnBlur="JavaScript:return chnumtel2(this.value);"></td>
</tr>
<tr>
<td>รายละเอียดอื่นๆ :</td>
<td> <textarea cols="50" rows="5" name="Description" ></textarea></td>
</tr>
</table>
<br>
<fieldset>
<legend> Account ของลูกค้า </legend>
<table border='0' style="width:70% ">
<tr>
<td>Username </td>
<td> : <input name="username" size="20" type="text" placeholder="Username" OnBlur="JavaScript:return chUsername(this.value);"></td>
</tr>
<tr>
<td>Password </td>
<td> : <input name="password" size="20" type="text" placeholder="Password" OnBlur="JavaScript:return chPassword(this.value);"></td>
</tr>
<tr>
<td>Re-Password </td>
<td> : <input name="RePassword" size="20" type="text" placeholder="RePassword"></td>
</tr>
</table>
</fieldset>
</fieldset>
<center>
<br/><br/><input name="submit1" type="submit" value="เพิ่มข้อมูลลูกค้า" >
<input name="goback_form" type="reset" value="ย้อนกลับ" onclick='goback();'><br>
</center>
</td></tr></table>
</center>
</form>
</body>
</html>
หน้าต่างในการ ลบ ข้อมูลจากฐานข้อมูล
<?php
$con = mysql_connect("localhost","root","1234");
$conn = mysql_select_db("mystore",$con);
mysql_query("SET NAMES UTF8");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
<head>
<title>แก้ไขข้อมูลลูกค้า</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=UTF-8">
<link rel="stylesheet" href="style1.css">
<script language="javascript">
function goback(){
window.location = 'Lab5_55523206055-1.php';
}
</script>
</head>
<body>
<?php
if(isset($_GET["id"])){
$sql = "select * from customer where Customer_id = $_GET[id]";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
}
if(isset($_POST["ok"])){
$sql = "delete from customer WHERE Customer_id = $_GET[id]";
if(mysql_query($sql)){
echo "<script>window.location = 'Lab5_55523206055-1.php'</script>";
}
}
?>
<form id="delForm" method="post" action="">
<center>
<table border='0' style="width:40%">
<tr><td>
<h3 style="color:red"><u>ลบข้อมูลลูกค้า</u></h3>
<fieldset style="color:black" width="500px"height="40%" >
<legend> ข้อมูลลูกค้า </legend>
<table border='0' style="width:80%">
<tr>
<td>ID </td>
<td> : <?php echo $row['Customer_id']; ?></td>
</tr>
<tr>
<td>ชื่อ </td>
<td> : <?php echo $row['Customer_Name']; ?></td>
</tr>
<td>นามสกุล </td>
<td> : <?php echo $row['Customer_Lastname']; ?></td></tr>
<tr>
<td>เพศ </td>
<td> : <?php echo $row['Gender']; ?></td></tr>
<tr>
<td>วัน-เดือน-ปี เกิด </td>
<td> : <?php echo $row['Birthdate']; ?></td></tr>
<tr>
<td>ที่อยู่ </td>
<td> : <?php echo $row['Address']; ?></td></tr>
<tr>
<td>จังหวัด </td>
<td> : <?php echo $row['Province']; ?></td></tr>
<tr>
<td>รหัสไปรษณีย์ </td>
<td> : <?php echo $row['Zipcode']; ?></td></tr>
<tr>
<td>โทรศัพท์ </td>
<td> : <?php echo $row['Telephone']; ?></td></tr>
<tr>
<td>รายละเอียดอื่นๆ </td>
<td> : <?php echo $row['Customer_Description']; ?></td></tr>
</table>
</fieldset>
<br />
<center>
<input name="ok" type="submit" value="ลบ" onclick="return confirm('ยืนยันการลบ')">
<input name="goback_form" type="reset" value="ย้อนกลับ" onclick='goback();'>
<br>
</center>
</form>
</body>
</html>
หน้าต่างการแก้ไข ข้อมูล
<?php
$con = mysql_connect("localhost","root","1234");
$conn = mysql_select_db("mystore",$con);
mysql_query("SET NAMES UTF8");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
<head>
<title>แก้ไขข้อมูลลูกค้า</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=UTF-8">
<link rel="stylesheet" href="style1.css">
<script language="javascript">
function goback(){
window.location = 'Lab5_55523206055-1.php';
}
</script>
</head>
<body>
<form name="form1" id="edForm" method="post" action="" onSubmit="JavaScript:return fncSubmit();">
<?php
if(isset($_GET["id"])){
$sql = "select * from customer where Customer_id = $_GET[id]";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
}
if(isset($_POST["save"])){
$year_now = date("Y");
$year_birth = substr($_POST['date'],0,4);
$age = $year_now - $year_birth;
$sql = "update customer
SET Customer_Name = '$_POST[name1]',
Customer_Lastname = '$_POST[lastname]',
Gender = '$_POST[Gender]',
Age = '$age',
Birthdate = '$_POST[date]',
Address = '$_POST[address]',
Province = '$_POST[province]',
Zipcode = '$_POST[zipcode]',
Telephone = '$_POST[telephone]',
Customer_Description = '$_POST[Description]'
WHERE Customer_id = $_GET[id]";
if(mysql_query($sql)){
echo "<script>window.location = 'Lab5_55523206055-1.php'</script>";
}
}
?>
<center>
<table border='0' style="width:50%">
<tr><td>
<h3 style="color:red"><u>แก้ไขข้อมูลลูกค้า</u></h3>
<fieldset style="color:black" width="300px"height="40%">
<legend> ข้อมูลลูกค้า </legend>
<table border='0' style="width:100%">
<tr>
<td>ชื่อ </td>
<td> : <input name="name1" type="text" size="20" placeholder="ชื่อภาษาไทย" value="<?php echo $row['Customer_Name']; ?>"></td>
</tr>
<tr>
<td>นามสกุล </td>
<td> : <input name="lastname"type="text" size="20" placeholder="นามสกุลภาษาไทย" value="<?php echo $row['Customer_Lastname']; ?>"></td>
</tr>
<tr>
<td>เพศ </td>
<td> : <?php $Check =$row['Gender'];
if ($Check=="ชาย") {
echo "<input type=radio name=Gender value=ชาย Checked=Checked>ชาย <input type=radio name=Gender value=หญิง>หญิง";
}elseif ($Check=="หญิง"){
echo "<input type=radio name=Gender value=ชาย >ชาย <input type=radio name=Gender value=หญิง Checked=Checked>หญิง";
}
else {
echo "<input type=radio name=Gender value=ชาย >ชาย <input type=radio name=Gender value=หญิง >หญิง";
}
?>
</tr>
<tr>
<td>วัน-เดือน-ปี-เกิด </td>
<td> : <input class=other type="date" name="date" value="<?php echo $row['Birthdate']; ?>"></td>
</tr>
<tr>
<td>ที่อยู่ </td>
<td> : <input name="address"type="text" size="40" value="<?php echo $row['Address']; ?>" ></td>
</tr>
<tr>
<td>จังหวัด </td>
<td> : <select name="province" >
<option value='<?php echo $row['Province']; ?>'><?php echo $row['Province']; ?></option>
<option value="เชียงราย">เชียงราย</option>
<option value="เชียงใหม่">เชียงใหม่</option>
<option value="น่าน">น่าน</option>
<option value="พะเยา">พะเยา</option>
<option value="แพร่">แพร่</option>
<option value="แม่ฮ่องสอน">แม่ฮ่องสอน</option>
<option value="ลำปาง">ลำปาง</option>
<option value="ลำพูน">ลำพูน</option>
<option value="อุตรดิตถ์">อุตรดิตถ์</option>
</select></td>
</tr>
<tr>
<td>รหัสไปรษณีย์ </td>
<td> : <input name="zipcode"type="text" size="5" value="<?php echo $row['Zipcode']; ?>"></td>
</tr>
<tr>
<td>เบอร์โทรศัพท์ </td>
<td> : <input name="telephone"type="text" size="20" value="<?php echo $row['Telephone']; ?>"></td>
</tr>
<tr>
<td>รายละเอียดอื่นๆ :</td>
<td> <textarea cols="50" rows="5" name="Description" ><?php echo $row['Customer_Description']; ?></textarea></td>
</tr>
</table>
<br>
</fieldset>
<br />
<center>
<button name="save" type="submit" value="แก้ไข" >แก้ไข</button>
<input name="goback_form" type="reset" value="ย้อนกลับ" onclick='goback();'><br>
</center>
</td></tr></table>
</center>
</form>
</body>
</html>