  | 
              
	              
	                
  
    
	 
        วิธีลบข้อมูล 5 ตารางที่ join กัน ผมทำแบบนี้ถูกไหมครับช่วยดูหน่อยครับ ทำไมไม่ได้     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                ช่วยดูให้หน่อยครับลบข้อมูลในตาราง 5 ตารางที่ join กัน 
ตารางมีทั้งหมด 5 ตารางครับที่ต้องการลบ ทั้ง5 ตารางนี้ join std_id ทั้ง 5 ตารางครับ 
ตาราง ชื่อ student, status_student, score, pro_topten, pre_topten 
 
 
 
ส่วนแสดงข้อมูล 
Code (PHP) 
<?php
    include("../config.php"); 
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Local History Southern Thailand </title>
        <link rel="stylesheet" href="../css/">
    </head>
    <body>
        <h2>รายชื่อนักนักศึกษา</h2>
        <table width="20%"  align="center">
            <tr>
                <th>ลบรายชื่อทั้งหมด</th>
                <td>ลบ</td>
            </tr>
        </table>
        <table width="90%" border="1" align="center">
        <tr>
            <th>Username</th>
            <th>Password</th>
            <th>รหัสนักศึกษา</th>
            <th>ชื่อ</th>
            <th>สกุล</th>
            <th></th>
            <th></th>
        </tr>
    <?php
    $sqli = "SELECT * FROM student order by codestudent DESC";
    $query = mysqli_query($con, $sqli) or die("Error (".$sqli.")");
    while($row = mysqli_fetch_array($query)){
    ?>
        <tr>
            <td hidden="hidden"><?php echo $row["std_id"] ?></td>
            <td><?php echo $row["user_id"] ?></td>
            <td><?php echo $row["user_password"]?></td>
            <td><?php echo $row["codestudent"]?></td>
            <td><?php echo $row["username"]?></td>
            <td><?php echo $row["lastname"]?></td>
            <td><a href="edit_student.php?std_id=<?php echo $row["std_id"]?>">แก้ไข</a></td>
            <td><a href="delete_student.php?std_id=<?php echo $row["std_id"]?>" onclick='return confirm("คุณแน่ใจที่จะลบ")'>ลบ</a></td>
        </tr>
        <?php } ?>
        </table>
 
    </body>
</html>
<?php
	mysqli_close($con);
?>
 
 
 
 
ส่วนคำสั่งลบ 
Code (PHP) 
<?php 
    session_start();
    include("../config.php");  
    $std_id = $_GET['std_id'];
?>
   
<?php
	$sql = "DELETE FROM 'student', 'status_student', 'score', 'pro_topten', 'pre_topten'
        USING 'student'
            INNER JOIN 'status_student' 
            INNER JOIN 'score'
            INNER JOIN 'pro_topten'
            INNER JOIN 'pre_topten'
        WHERE 'student'.'std_id' = '$std_id'
            AND 'status_student' .'user_id' = 'student'.'id'
            AND 'score'.'user_id' = 'student'.'id'
            AND 'pro_topten'.'user_id' = 'student'.'id'
            AND 'pre_topten'.'user_id' = 'student'.'id'";
	$query = mysqli_query($con,$sql) or die ("ไม่สามารถลบได้ : (".$sql.")");
	echo"<script>alert('ลบข้อมูลเรียบร้อยแล้ว');</script>";
	echo"<script>window.location='search_pretest.php';</script>";
?>
 
 
  Tag : PHP               
                        | 
           
          
            | 
			
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2018-04-14 22:31:01 | 
                      By :
                          tonmio852 | 
                      View :
                          1348 | 
                      Reply :
                          6 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
$sql="DELETE s1,s2,s3, p1,p2
FROM student s1, status_student s2, score s3, pro_topten p1, pre_topten p2
where s1.std_id = $std_id
AND s2.user_id = s1.id
AND s3.user_id = s1.id
AND p1.user_id = s1.id
AND p2.user_id = s1.id";
  
 
 
แนะนำให้ทำ foreign key delete cascade ไว้จะดีกว่า ลบ main ทีเดียวเลย                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-04-15 04:12:57 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ยังลบไม่ได้เลยครับ ขึ้นแจ้งเตือนแบบนี้ 
  
Code (PHP) 
    $sql="DELETE s1, s2, s3, p1, p2
    FROM student s1, status_student s2, score s3, pro_topten p1, pre_topten p2
    where s1.std_id = $std_id
        AND s2.std_id = s1.std_id
        AND s3.std_id = s1.std_id
        AND p1.std_id = s1.std_id
        AND p2.std_id = s1.std_id";
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-04-15 22:19:34 | 
                        By :
                            tonmio852 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 https://dev.mysql.com/doc/refman/5.7/en/delete.html 
 
ลองเอาไปรันบน phpmyadmin ดูว่าเขาแจ้งอะไรดีกว่าครับ โค๊ดแจ้งเออเร่อร์ในโปรแกรมที่เขียนเองไม่สมบูรณ์ 
ก็จะแก้ไขยากเพราะต้องมางมเอง 
 
ที่ถูกต้องควรเขียนเขียนแบบนี้ 
 
$query = $con->query($sql) or die ('ไม่สามารถลบได้ :<br>'. $con->error . '<br>' . $sql);                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-04-15 23:18:15 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                   
 
  
 
เวลาสั่งลบให้ ลบ ตารางแม่ ตารางเดียว ตารางลูกที่เกี่ยวข้องจะถูกลบด้วย 
 
 
ส่วนโค๊ดผม ที่ทำไว้ ลบไม่ได้ เพราะมีการทำ relation เอาไว้แล้ว  
ถ้าผิดการอ้างอิง ก็จะไม่ให้ลบ ก็ต้อง จัดวางลำดับการ ลบให้ถูกต้อง 
เอาแบบตามรูปภาพดีกว่าครับ ชัดเจน                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-04-16 07:37:39 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |