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

การทำการติดต่อฐานข้อมูล 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");
?>

ไม่มีความคิดเห็น:

แสดงความคิดเห็น