สอบถาม หรือ ขอโค้ด เพื่อแสดงข้อมูลผู้ login ค่ะ

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ น่ะค่ะ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ

Moderator: phpbb, mindphp, ผู้ดูแลกระดาน

ตอบกลับโพส
ภาพประจำตัวสมาชิก
moccalate
phpBBThailand Newbie
phpBBThailand Newbie
โพสต์: 1
ลงทะเบียนเมื่อ: 26 ธ.ค. 2015, 17:34

สอบถาม หรือ ขอโค้ด เพื่อแสดงข้อมูลผู้ login ค่ะ

โพสต์ โดย moccalate »

ก่อนอื่น หนูขอบอกก่อนนะคะ ว่าหนู แทบไม่มีความรู้เรื่อง php เลย ตอนนี้ หนูได้ลองโค้ด 2 ชุดด้วยกัน ซึ่งทั้งสองโค้ดสามารถทำได้เหมือนกันคือ login ไปยังหน้าที่ต้องการได้
ฐานข้อมูล แบ่งออกเป็น 3 ตารางค่ะ เช่น
1. บุคคลทั่วไป มีการเก็บข้อมูลส่วนตัว และ ไอดี รหัสผ่าน
2. พนักงาน มีการเก็บข้อมูลส่วนตัว และ ไอดี รหัสผ่าน
3. แอดมิน มีการเก็บข้อมูลส่วนตัว และ ไอดี รหัสผ่าน
โค้ดที่ใช้นะคะ
checklogin

โค้ด: เลือกทั้งหมด

<?
include"connect.php";
$sql1 = "select * from admin where ID_admin = '$_POST[username]' and password='$_POST[password]' ";
$dbquery1=mysql_query("set names utf8");
$dbquery1 = mysql_query($sql1);
$data1 = mysql_fetch_array($dbquery1);
$rows1 = mysql_num_rows($dbquery1);

$sql2 = "select * from employee where ID_employee = '$_POST[username]' and password='$_POST[password]' ";
$dbquery2=mysql_query("set names utf8");
$dbquery2 = mysql_query($sql2);
$data2 = mysql_fetch_array($dbquery2);
$rows2 = mysql_num_rows($dbquery2);


$sql3 = "select * from normal where ID_normal = '$_POST[username]' and password='$_POST[password]' ";
$dbquery3=mysql_query("set names utf8");
$dbquery3 = mysql_query($sql3);
$data3 = mysql_fetch_array($dbquery3);
$rows3 = mysql_num_rows($dbquery3);


if($rows1==1){
	echo"เข้าระบบได้สำเร็จ ";
$_SESSION['name']=$data1['name'];//กำหนดแสดงส่วนที่ต้องการเช่น ชื่อ ตอนล็อกอิน
$_SESSION['surname']=$data1['surname'];


	echo"<meta http-equiv='refresh' content='1;url=admin.php' >";//แยกหน้าUSER
}

else if($rows2==1){
	echo"เข้าระบบได้สำเร็จ ";
$_SESSION['name']=$data2['name'];
$_SESSION['surname']=$data2['surname'];
	echo"<meta http-equiv='refresh' content='1;url=employee.php' >";//แยกหน้าUSER
}
else if($rows3==1){
echo"เข้าระบบได้สำเร็จ ";
$_SESSION['name']=$data3['name'];
$_SESSION['surname']=$data3['surname'];

	echo"<meta http-equiv='refresh' content='1;url=normal.php' >";
}
else {
	echo"ไม่สามารถเข้าระบบได้ โปรดตรวจสอบ Login และ  Password ";
	echo"<meta http-equiv='refresh' content='3;url=login.php' >";
}
 ?>
อันนี้คืออีกโค้ดที่ลองค่ะ

โค้ด: เลือกทั้งหมด

<?php
include"connect.php";
mysql_query("set names utf8");

$username = mysql_real_escape_string($_POST['username']);
$password =  mysql_real_escape_string($_POST['password']);

$sql = "select * from admin where ID_admin = '$username' and password='$password' ";
$rs = mysql_query($sql);
if( mysql_num_rows($rs) ){
	$_SESSION['profile']=mysql_fetch_assoc($rs);
	$_SESSION['type']='admin';
	echo"<meta http-equiv='refresh' content='1;url=admin.php' >Login สำเร็จ";//แยกหน้าUSER
}else{
	$sql = "select * from employee where ID_employee = '$username' and password='$password' ";
	$rs = mysql_query($sql);
	if(mysql_num_rows($rs)){
		$_SESSION['profile']=mysql_fetch_assoc($rs);
		$_SESSION['type']='employee';
		echo"<meta http-equiv='refresh' content='1;url=employee.php' >Login สำเร็จ";
	}else{
		$sql = "select * from normal where ID_normal = '$username' and password='$password' ";
		$rs = mysql_query($sql);
		if(mysql_num_rows($rs)){
			$_SESSION['profile']=mysql_fetch_assoc($rs);
			$_SESSION['type']='normal';
			echo"<meta http-equiv='refresh' content='1;url=normal.php' >Login สำเร็จ";
		}else{
			echo"ไม่สามารถเข้าระบบได้ โปรดตรวจสอบ Login และ  Password ";
			echo"<meta http-equiv='refresh' content='3;url=login.php' >";
			exit;			
		}
	}
}
?>
** หมายเหตุ include"connect.php" คือหน้า connect ฐาน
## ทั้งสองโค้ด สามารถ login ไปยังหน้าที่ต้องการได้แล้ว แต่จะทำอย่างไร ให้สามารถแสดงข้อมูลส่วนตัวของผู้ login ได้คะ เช่น รหัสประจำตัว ชื่อ นามสกุล ของผู้ login ในหน้า admin.php normal.php employee.php
ภาพประจำตัวสมาชิก
phpbb
phpBBThailand VIP Members
phpBBThailand VIP Members
โพสต์: 3189
ลงทะเบียนเมื่อ: 19 มิ.ย. 2008, 00:26
ติดต่อ:

Re: สอบถาม หรือ ขอโค้ด เพื่อแสดงข้อมูลผู้ login ค่ะ

โพสต์ โดย phpbb »

สอบถามเรื่องโปรแกรม ที่หมวด นี้ครับ
http://www.mindphp.com/forums/viewforum.php?f=6
โหลด phpBB3 ภาษาไทย ทั้งหน้าเว็บ และ admin
รวมพลคนใช้ phpBB3 แนะนำคนอื่นบ้างนะครับ ทุกคำถามจะได้มีคำตอบ แนะนำคนอื่นๆบ้างนะ ช่วยๆกันไป

สมาชิกใหม่ ก่อนถามแนะนำตัว
http://www.phpbbthailand.com/viewforum.php?f=41
กฏการใช้งานบอร์ด
http://www.phpbbthailand.com/viewtopic.php?f=37&t=5940
ตอบกลับโพส

ย้อนกลับไปยัง

ผู้ใช้งานขณะนี้

กำลังดูบอร์ดนี้: 29 และ บุคคลทั่วไป 0 ท่าน