繁体   English   中英

如何在 php 中的一个 mysql 查询中从 2 个表中删除数据?

[英]How to delete data from 2 tables in one mysql query in php?

我需要删除一个具有特定 ID 的国家和属于该国家的城市。 我在堆栈溢出中查找了这个并想出了这个,但它仍然没有执行它..

if(isset($_POST['id'])){
        $id =($_POST['id']);}

    $connection = mysqli_connect("localhost", "root", "", "lol");
    if ($connection-> connect_error) {
        die("Connection failed:". $connection-> connect_error);
    }
    $sql= "DELETE `countries`, `cities` FROM `countries` INNER JOIN `cities` on 
    `cities.fk_Country` = `countries.id` WHERE `countries.id` = $id";
    var_dump($sql);
    if(!mysqli_query($connection,$sql)){
        echo 'Unable to delete the country. Try again!';
    } else {
        echo 'Succesfully deleted the country!';
    }

首先删除您的cities ,因为它具有countries地区的外键。

$sql= "delete cities, countries
     from cities
     inner join countries on cities.fk_Country = countries.id
     where countries.id=$id";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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