  | 
              
	              
	                
  
    
	 
        รบกวนช่วยดู code ตรวจสอบค่าว่างให้หน่อยครับว่าติดปัญหาตรงไหน     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                คือ ตาม code ผมต้องการให้มีการตรวจสอบค่าว่าตอน Login น่ะครับ ซึ่งถ้าเป็นในส่วนของ email มีค่าว่าง มันก็จะเตือนขึ้นมาว่า "กรอก email" เเต่พอเป็นในส่วนของ password มีค่าว่าง มันกลับเด้งไปทำตรง else บรรทัดสุดท้ายเลยครับ "Email หรือ Password ไม่ถูกต้อง" ซึ่งผมไม่เเน่ใจว่ามันผิดตรงส่วนไหน 
 
Code (PHP) 
<?php
include 'connect.php';
$cus_email = mysqli_real_escape_string($con,$_POST['cus_email']);
$cus_password = mysqli_real_escape_string($con, md5($_POST['cus_password']));
// ตรวจสอบว่าชื่อ Username ไม่เป็นค่าว่างๆ
if ($cus_email == '') {
        echo "<script>alert('กรอก Email ');window.location='Login.php';</script>";
    } elseif ($cus_password == '') {
	echo "<script>alert('กรอก Password ');window.location='Login.php';</script>";
} else {
    // ตรวจสอบกับฐานข้อมูลว่า Username และรหัสผ่านถูกต้อง
$sql = "SELECT * FROM customer WHERE cus_email=? AND cus_password=?";
$stmt = mysqli_prepare($con, $sql);
mysqli_stmt_bind_param($stmt, "ss", $cus_email , $cus_password);
mysqli_execute($stmt);
$result_user = mysqli_stmt_get_result($stmt);
if($result_user->num_rows == 1){
    session_start();
    $row_user = mysqli_fetch_array($result_user,MYSQLI_ASSOC);
    $_SESSION['cus_id'] = $row_user['cus_id'];
    
    $_SESSION['cus_status'] = $row_user['cus_status'];
        if($_SESSION["cus_status"]==0){ //ถ้าเป็น user ให้กระโดดไปหน้า admin_page.php
        header("Location: main1.php");
    }elseif ($_SESSION["cus_status"]==1) { //ถ้าเป็น admin ให้กระโดดไปหน้า admin_page.php
        header("Location: main2.php");
    }elseif ($_SESSION["cus_status"]==2) { //ถ้าเป็น Boss ให้กระโดดไปหน้า admin_page.php
        header("Location: main3.php");
    }
    
} else {
    echo "<script>alert('Email หรือ Password ไม่ถูกต้อง');window.location='Login.php';</script>";
        }
        }
?>
 
 
  Tag : PHP, HTML5, JavaScript, Ajax, jQuery               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2018-11-11 10:57:58 2018-11-11 16:51:10 2018-11-11 16:52:12	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2018-11-10 23:32:28 | 
                      By :
                          worapong39 | 
                      View :
                          1005 | 
                      Reply :
                          13 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ครอบแท็ก php ได้ไหมครับ? 
                         
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-11-11 06:05:49              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-11-11 06:04:34 | 
                        By :
                            mr.v | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Mr.V หมายถีงว่า ให้จขกท คลิก icon tag php ตามในรุป คือ รูปแรก ก่อนที่จะกดปุ่ม ctrl+v เพื่อ paste ข้อความ 
 
เวลาคนเข้ามาดูก็จะเป็น สีสัน อ่านได้ง่าย                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-11-11 16:36:05 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลองศึกษาดูครับ เขียนเป็นตัวอย่างให้เลย เขียนแบบ oop จะสั้นและง่ายกว่า ฟังก์ชั่นแบบเดียวกัน แต่สั้นกว่า 
Code (PHP) 
<?php
include 'connect.php';
$cus_email = isset($_POST['cus_email']) ? trim($_POST['cus_email']) : ''; // ตรวจสอบและกำหนดค่า
$cus_paswd = isset($_POST['cus_password']) ? md5(trim($_POST['cus_password'])) : ''; //ตรวจสอบและกำหนดค่า
// ตรวจสอบว่าชื่อ Username ไม่เป็นค่าว่างๆ
if (strlen($cus_email) < 1) {
	echo "<script>alert('กรอก Email ');window.location='Login.php';</script>"; 
	exit; // ใส่ exit เพื่อจบโปแรกรม
}elseif (strlen($cus_paswd) < 1) {
	echo "<script>alert('กรอก Password ');window.location='Login.php';</script>"; 
	exit; // ใส่ exit เพื่อจบโปแรกรม
}
// ตรวจสอบกับฐานข้อมูลว่า Username และรหัสผ่านถูกต้อง
$sql = "SELECT * FROM customer WHERE cus_email=? AND cus_password=?";
$stmt = $con->prepare($sql);
$stmt->bind_param('ss', $cus_email , $cus_paswd); // function  นี้ ไม่จำเห็นต้อง escape มาก่อน เพราะจัดการให้แล้ว
// escape ซ้ำซ้อน อาจทำให้มีปัญหาได้
$stmt->execute();
$result_user = $stmt->get_result();
if($result_user->num_rows<1){
	echo "<script>alert('Email หรือ Password ไม่ถูกต้อง');window.location='Login.php';</script>"; 
	exit;
}
session_start();
$row_user = $result_user->fetch_object(); // ลองหัดใช้แบบ oop ทั้งหมดดู สั้งงกว่า ง่ายกว่า เร็วกว่า
$_SESSION['cus_id'] = $row_user->cus_id;
$_SESSION['cus_status'] = $row_user->cus_status;
// อะไรที่เข้า ฟังก์ชั่นได้ก็เข้าซะ ไม่ต้องใช้ if else ให้ข้าและยาวไปอีก 
header('Location: main'.($row_user->cus_status+1).'.php');
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-11-11 17:02:35 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ต้องแสดง ช่อง กรอก Password                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-11-11 18:27:06 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 debug ทีละบันทัดเลยครับ 
 
Code (PHP) 
$email = .......;
echo strlen($email); exit; //ถ้าผ่าน ก็ลบออก ไป ตรวจ password รายการต่อไป จะได้รู้ว่า ข้อมูลมันเป็นอะไรแน่
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-11-11 18:43:50 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
$cus_paswd = isset($_POST['cus_password']) && !empty(trim($_POST['cus_password'])) ? md5(trim($_POST['cus_password'])) : '';
  
 
 
ส่วนของตรวจ password form ก็... 
Code (PHP) 
elseif (empty($cus_paswd)) {
	echo "<script>alert('กรอก Password ');window.location='Login.php';</script>"; 
	exit; // ใส่ exit เพื่อจบโปแรกรม
}
 
 
ทั้งนี้แนะนำให้ศึกษาฟังก์ชั่น password_hash password_verify ซึ่งความปลอดภัยห่างชั้นดีกว่าเหนือกว่า md5 มาก.                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-11-11 20:25:38              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-11-11 20:24:07 | 
                        By :
                            mr.v | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
      		  
	
     | 
   
 
                 |