แก้กันจนตาลายแล้วช่วยหน่อยครับ พอดีมือใหม่ด้วยครับ
ชื่อตาราง personnel
username = trainee
password = 1234
status = trainee
___________________________________________________
โค้ดหน้า login.php
<?php
session_start();
// ติดต่อฐานข้อมูล
include("connect.php");
// รับค่า username และ password จากฟอร์ม
$username = $_POST['username']; // รับ username เข้ามา
$password = $_POST['password']; // รับ password เข้ามา
// เช็คว่าผู้ใช้คลิ๊กปุ่ม submit หรือยัง
if($_POST['submit'] == "Login"){
// เช็คว่าป้อนค่าในฟอร์มครบหรือยัง (ฟอร์มทั้ง 2 ช่อง ต้องไม่เป็นค่าว่าง)
if($username != "" and $password != ""){
// เขียนคำสั่ง sql ดึงข้อมูล email กับ password จากตาราง registermember
$sql = "SELECT * FROM personnel WHERE username='$username' and password='$password'";
$sqlquery = mysql_query($sql);
// นับแถวเพื่อเช็คว่ามี username และ password นี้อยู่หรือไม่
$num = mysql_num_rows($sqlquery);
// ดึงข้อมูลตามฟิลด์
$data = mysql_fetch_array($sqlquery);
// เช็คว่า username และ password ที่ป้อนเข้ามาถูกต้องหรือไม่
if($num >=1){
// สร้างตัวแปร session
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$_SESSION['lname'] = $data[lname];
$_SESSION['sname'] = $data[sname];
// ส่งไปหน้า member.php
header("location:check_login.php");
}else{
echo "<center><font color=red>คุณป้อน username หรือ password ไม่ถูกต้อง ลองใหม่!!</font></center>";
}
}else{ //ถ้าฟอร์มมีค่าว่าง
echo "<center><font color=red>กรุณาป้อนข้อมูลให้ครบก่อน</font></center>";
}
}
?>
<!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>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
Login to SystemWork<br>
<table border="1" style="width: 300px">
<tbody>
<tr>
<td> Username</td>
<td>
<input name="username" type="text" id="username">
</td>
</tr>
<tr>
<td> Password</td>
<td><input name="password" type="password" id="password">
</td>
</tr>
</tbody>
</table>
<br>
<input type="submit" name="submit" value="Login" id="submit">
</form>
</body>
</html>
______________________________________________________________
โค้ดหน้า check_login.php
<?
include("connect.php");
session_start();
$username = $_POST['username'] ;
$password = $_POST['password'] ;
$sql = "select * from personnel where username = '$username' and password = '$password' ";
$result = mysql_db_query($dbname,$sql);
$row = mysql_num_rows($result);
$record = mysql_fetch_array($result);
$strUser = $username;
/*if ($row == 0) {
echo "<script> alert('');
location.href = 'login.php';
</script>";
exit;
} else {*/
if($record[STATUS]=='staff'){
session_register("strUser");
echo "<script>location.href='staff.php';</script>";
}else if ($record[STATUS]=='trainee') {
session_register("strUser");
echo "<script>location.href='trainee.php';</script>";
} /*else {
echo "<script>location.href='login.php';</script>";
}*/
/*}*/
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
______________________________________________________________________________
โค้ดมันเด็งเข้าหน้า Check_login.php แต่มันไม่ยอมเช็คสถานะครับ มาจะมาหยุดอยู่หน้า Check_login.php
ช่วยหน่อยครับ
ช่วยแก้โค้ด PHP หน่อยครับ
ชื่อตาราง personnel
username = trainee
password = 1234
status = trainee
___________________________________________________
โค้ดหน้า login.php
<?php
session_start();
// ติดต่อฐานข้อมูล
include("connect.php");
// รับค่า username และ password จากฟอร์ม
$username = $_POST['username']; // รับ username เข้ามา
$password = $_POST['password']; // รับ password เข้ามา
// เช็คว่าผู้ใช้คลิ๊กปุ่ม submit หรือยัง
if($_POST['submit'] == "Login"){
// เช็คว่าป้อนค่าในฟอร์มครบหรือยัง (ฟอร์มทั้ง 2 ช่อง ต้องไม่เป็นค่าว่าง)
if($username != "" and $password != ""){
// เขียนคำสั่ง sql ดึงข้อมูล email กับ password จากตาราง registermember
$sql = "SELECT * FROM personnel WHERE username='$username' and password='$password'";
$sqlquery = mysql_query($sql);
// นับแถวเพื่อเช็คว่ามี username และ password นี้อยู่หรือไม่
$num = mysql_num_rows($sqlquery);
// ดึงข้อมูลตามฟิลด์
$data = mysql_fetch_array($sqlquery);
// เช็คว่า username และ password ที่ป้อนเข้ามาถูกต้องหรือไม่
if($num >=1){
// สร้างตัวแปร session
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$_SESSION['lname'] = $data[lname];
$_SESSION['sname'] = $data[sname];
// ส่งไปหน้า member.php
header("location:check_login.php");
}else{
echo "<center><font color=red>คุณป้อน username หรือ password ไม่ถูกต้อง ลองใหม่!!</font></center>";
}
}else{ //ถ้าฟอร์มมีค่าว่าง
echo "<center><font color=red>กรุณาป้อนข้อมูลให้ครบก่อน</font></center>";
}
}
?>
<!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>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
Login to SystemWork<br>
<table border="1" style="width: 300px">
<tbody>
<tr>
<td> Username</td>
<td>
<input name="username" type="text" id="username">
</td>
</tr>
<tr>
<td> Password</td>
<td><input name="password" type="password" id="password">
</td>
</tr>
</tbody>
</table>
<br>
<input type="submit" name="submit" value="Login" id="submit">
</form>
</body>
</html>
______________________________________________________________
โค้ดหน้า check_login.php
<?
include("connect.php");
session_start();
$username = $_POST['username'] ;
$password = $_POST['password'] ;
$sql = "select * from personnel where username = '$username' and password = '$password' ";
$result = mysql_db_query($dbname,$sql);
$row = mysql_num_rows($result);
$record = mysql_fetch_array($result);
$strUser = $username;
/*if ($row == 0) {
echo "<script> alert('');
location.href = 'login.php';
</script>";
exit;
} else {*/
if($record[STATUS]=='staff'){
session_register("strUser");
echo "<script>location.href='staff.php';</script>";
}else if ($record[STATUS]=='trainee') {
session_register("strUser");
echo "<script>location.href='trainee.php';</script>";
} /*else {
echo "<script>location.href='login.php';</script>";
}*/
/*}*/
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
______________________________________________________________________________
โค้ดมันเด็งเข้าหน้า Check_login.php แต่มันไม่ยอมเช็คสถานะครับ มาจะมาหยุดอยู่หน้า Check_login.php
ช่วยหน่อยครับ