删除内部联接

时间:2015-12-01 12:24:45

标签: php mysql inner-join delete-row

我正在尝试创建一个视频共享网站,我正在尝试在我的管理页面上创建DELETE INNER JOIN,这样当管理员删除用户时,它将同时删除所有上传的用户上传的视频。

当我点击它时没有任何反应。

我的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

2 个答案:

答案 0 :(得分:1)

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 |
+---------+----------+

答案 1 :(得分:0)

delete_id更改为$delete_id

试试这段代码: -

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