วันพฤหัสบดีที่ 14 พฤษภาคม พ.ศ. 2558

การเปิดการติดต่อกับฐานข้อมูล Mysql


การเปิดการติดต่อกับฐานข้อมูล Mysql

• mysql_connect(Servername,Username,Password); Servername ชื่อ Server หรือ IP Address Username ชื่อบัญชี Password รหัสผ่าน

EX
$con = mysql_connect($host,$user,$pass);
if(!$con)
die(“ไม่สามารถตด ิ ต่อฐานข้อมูลได้”);
การเลือกฐานข้อมูลใช้งาน
mysql_select_db(DataBaseName, Connection);
DataBaseName ชื่ อฐานข้อมูล

Connection ตัวแปรใช้ส าหรับรับค่า Connection

EX
mysql_select_db(DataBaseName, Connection);
$dbname = “test”;
mysql_select_db($dbname,$con)
or die(“ไม่สามารถเลือกใช้งาน”.$dbname. “ได้”);

mysql_query();
mysql_query(SQLCode,Connection);
$sql = “select * from Province”;
$result = mysql_query($sql,$con);

Select Data

$con = mysql_connect(“localhost”, “root”, “1234”);
if(!$con)
die(“ไม่สามารถติดต่อฐานข้อมูลได้”);
mysql_select_db(“Customer”, $con);
$sql = “select * from customer”;
mysql_query("set NAMES utf8");
$result = mysql_query($sql,$con);
while($rs=mysql_fetch_array($result)){
echo $rs["customer_name"] . “<br>”;
}
mysql_close($con);


insert

if(isset($_POST[“custname”])){
$customer_name = $_POST[“custname”];
$sql=“insert into customer(customer_name)
value(‘“.$customer_name.”’)”;
mysql_query($sql,$con)
or die(“ไม่สามารถเพม ิ่ ข้อมูลได้<br>”.mysql_error());
}

update

if(isset($_GET[“id”]) && isset($_POST[“custname”])){
$customer_name = $_POST[“custname”];
$sql=“update customer set
customer_name=‘“.$customer_name.”’ where
customer_id = ‘“.$_GET[“id”].”’”;
mysql_query($sql,$con)
or die(“ไม่สามารถแก้ไขข้อมูลได้<br>”.mysql_error());
}

Delete

if(isset($_GET[“id”])){
$sql=“delete from customer where
customer_id = ‘“.$_GET[“id”].”’”;
mysql_query($sql,$con)
or die(“ไม่สามารถลบข้อมูลได้<br>”.mysql_error());
}

วันพุธที่ 13 พฤษภาคม พ.ศ. 2558

PHP Array Fn

PHP Array

การสร้างอาร์เรย์ผ่านฟังก์ชัน

 array();

$nick = array(“Som”, “Bom”, “Beer”, “Mew”); $animal = array(“Tiger”, “Dog”, “Lion”, “Elephant”);


กำหนดโดย Array Identifier

$array_name[]

$Variable[ ]
$animal[] = “Tiger”;
$animal[] = “Dog”;
$animal[] = “Lion”;


echo $animal[2] ; // Lion



$name = array(“Boy”, “Mint”, “Cat”, “Bom”);
foreach($name as $val)
{
echo $val . “<br>”;
}
Boy
Mint
Cat
Bom


ฟังก์ชันที่เกี่ยวกับ array
count() นับสมาชิกอาร์เรย์
array_merge() รวมอาร์เรย์
array_push() เพิ่มอาร์เรย์
sort() เรียงข้อมูล
String Function 
• strlen() จำนวนตัวอักษรของข้อความ 
• strpos() ค้นหาตำแหน่งของตัวอักษรในข้อความ 
• substr() ตัดข้อความ 
• str_replace() ค้นหาและแทนที่ข้อความ 
• strtolower() ทำข้อความเป็นตัวอักษรเล็ก 
• strtoupper() ทำข้อความเป็นตัวอักษรใหญ่ 
• ucfirst() ทำตัวอักษรแรกของข้อความเป็นตัวอักษรใหญ่
• ucwords() ทำตัวอักษรแรกของทุกคำในข้อความเป็นตัวอักษรใหญ่

ltrim() ตัดค่าว่างทางด้านซ้ายออก
rtrim() / chop() ตัดค่าว่างทางด้านขวาออก
trim() ตัดค่าว่างทั้งด้านซ้ายและขวาออก
chr() แปลงรหัส ASCII เป็ นตัวอักษร
ord() แปลงตัวอักษรเป็ นรหัส ASCII
strrev() เรียงสลับข้อความจากหลังไปหน้า
explode() แยกข้อความโดยใช้เครื่องหมายแยก (Array)
การคืนค่าจากฟังก์ชัน
function Multiply_Num($a)
{
return ($a*5);
}
$x = Multiply_Num(20);
echo $x; //100


การทำการติดต่อฐานข้อมูล mysql #02

หน้าต่างหน้าหลัก


<?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']."&nbsp;&nbsp;".$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="">&nbsp;&nbsp;&nbsp;---เลือก---</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>


การทำการติดต่อฐานข้อมูล mysql #01

สร้างหน้าต่าง login /logout

login

<?php
session_start();
    $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">
</head>
<body><br/>
<form  method='post' action="" name="frmlogin">
<CENTER>
<table border='0' style="width:25%">
   <tr><td>
<fieldset>
<legend>Login</legend>
<table border='0' style="width:100%">
<tr>
<td> Username </td>
    <td> : <input name="username" size="20" type="text" placeholder=" Username"/><br/></td> </tr>
<tr>
</tr>
<tr>
<td> Password </td>
    <td> : <input name="password" size="20" type="text" placeholder=" Password"/></td> </tr>
</table>
<center><br/>
<input name="login" type="submit" value="login" />
  <input name="reset_form" type="reset" value="cancel"><br>
</center>
</fieldset>
</td></tr>
</table>
</CENTER>
<?php
if(isset($_POST["login"])){
if($_POST["username"] != ""){
if($_POST["password"] != ""){
$sql = "select * from customer where username = '".$_POST['username']."' and password = '".$_POST['password']."'";
$query = mysql_query($sql)or die('Error ไม่สามารถเชื่อมต่อฐานข้อมูลได้ : '. mysql_error());
$row = mysql_fetch_array($query);
if(!$row){
}
else{
$_SESSION["User_id"] = $row["Customer_id"];
echo "<script>window.location = 'Lab5_55523206055-1.php'</script>";
}
} }echo "<CENTER><h5 style='color:red'>**Username and Password ไม่ถูกต้อง กรุณากลองใหม่ </5h></center>";
}
?>
</form>
</body>
</html>

logout

<?php
session_start();

session_destroy();
header("Location: ./login.php");
?>