简体   繁体   中英

Deleting data from multiple tables in a database

I've been trying to figure out how to delete data from multiple tables in a database. Just to give some background, there is a user table the holds basic information like first and last name. Then there is a login table that holds the passwords and usernames. I am making an admin page that allows me to see a user list with information from both tables. That part works fine. My problem is trying to create a delete script. I tried making separate queries, but I'm not getting anywhere. Can someone give me some direction? Or at least somewhere where I can read up on this specifically?

// Get IDs
$lid = $_POST['lid'];
$uid = $_POST['uid'];

// Delete the product from the database
require_once('database.php');

$query = "DELETE user,login FROM user INNER JOIN login ON user.uid = '$uid' AND login.lid= '$lid'";
 $db->exec($query);

添加外键然后在列定义中使用ON DELETE CASCADE,它将在删除父行时删除子行

It will depend on the database (and possibly what constraints are in place) whether you can do this in a single DML statement. The simpler option, though, would generally be to do something like this using two DML statements.

$query = "DELETE FROM user WHERE user.uid = '$uid'";
$db->exec($query);
$query = "DELETE FROM login WHERE login.lid= '$lid'";
$db->exec($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