简体   繁体   中英

Delete INNER JOIN

I'm trying so create a Video Sharing site, and I'm trying to create a DELETE INNER JOIN on my admin page, so that when the admin deletes a user it will at the same time delete all the uploaded videos that the user has uploaded.

Nothing happens then I click on it.

My php/html:

<?php
include("database/db_conection.php");
$view_users_query="select * from users ORDER BY user_name";
$run=mysqli_query($dbcon,$view_users_query);
while($row=mysqli_fetch_array($run))
{
    $user_id=$row[0];
    $user_name=$row[1];
    $user_email=$row[2];
    $user_pass=$row[3];
?>
<tr class="text-center">
    <td><?php echo $user_id;  ?></td>
    <td><?php echo $user_name;  ?></td>
    <td><?php echo $user_email;?></td>
    <td><?php echo $user_pass;  ?></td>
    <td class="center-block"><a href="delete.php?del=<?php echo $user_id ?>">
        <button class="btn btn-danger">Delete user</button></a></td>
</tr>
<?php } ?>

delete.php:

<?php
include("database/db_conection.php");
$delete_id=$_GET['del'];
$delete_query = "DELETE FROM users 
                 INNER JOIN videoes
                 ON users.user_email = videoes.user_name
                 WHERE users.user_email='delete_id'";//delete query
$run=mysqli_query($dbcon,$delete_query);
if($run)
{
    echo "<script>window.history.go(-1);</script>";
}
?>

Users table

用户表

Videoes table

视频表

DROP TABLE IF EXISTS users;

CREATE TABLE users(user_id INT NOT NULL PRIMARY KEY);

INSERT INTO users VALUES (1),(2),(3),(4),(5);

DROP TABLE IF EXISTS user_video;

CREATE TABLE user_video 
(user_id INT NOT NULL
,video_id INT NOT NULL
,PRIMARY KEY(user_id,video_id)
);

INSERT INTO user_video VALUES 
(1,101),(1,103),(1,105),(1,107),
(2,102),(2,104),(2,106),
(3,108),(3,109),
(4,110),(4,111),
(5,112),(5,113),(5,114),(5,115);

SELECT * 
  FROM users u 
  JOIN user_video uv 
    ON uv.user_id = u.user_id;

+---------+---------+----------+
| user_id | user_id | video_id |
+---------+---------+----------+
|       1 |       1 |      101 |
|       1 |       1 |      103 |
|       1 |       1 |      105 |
|       1 |       1 |      107 |
|       2 |       2 |      102 |
|       2 |       2 |      104 |
|       2 |       2 |      106 |
|       3 |       3 |      108 |
|       3 |       3 |      109 |
|       4 |       4 |      110 |
|       4 |       4 |      111 |
|       5 |       5 |      112 |
|       5 |       5 |      113 |
|       5 |       5 |      114 |
|       5 |       5 |      115 |
+---------+---------+----------+

DELETE u
     , uv
  FROM users u
  JOIN user_video uv 
    ON uv.user_id = u.user_id 
 WHERE u.user_id = 2;


SELECT * FROM users;
+---------+
| user_id |
+---------+
|       1 |
|       3 |
|       4 |
|       5 |
+---------+

SELECT * FROM user_video;
+---------+----------+
| user_id | video_id |
+---------+----------+
|       1 |      101 |
|       1 |      103 |
|       1 |      105 |
|       1 |      107 |
|       3 |      108 |
|       3 |      109 |
|       4 |      110 |
|       4 |      111 |
|       5 |      112 |
|       5 |      113 |
|       5 |      114 |
|       5 |      115 |
+---------+----------+

change delete_id to $delete_id

Try this code:-

$delete_query = "DELETE FROM users INNER JOIN videoes ON 
users.user_email = videoes.user_name 
WHERE users.user_email='$delete_id'";//delete query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM