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

标签 php mysql sql

我需要删除具有特定 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!';
    }

最佳答案

首先删除您的城市,因为它具有来自国家的外键。

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

关于php - 如何在php中的一个mysql查询中删除2个表中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58798261/

相关文章:

php - 具有大约 100 万个唯一关键字的表 utf_unicode 性能

php - 在将 csv 文件放入 php 数据库时,允许在 csv 文件中的字段中使用逗号

mysql - mysql数据库之间删除记录的方法

mysql - 在netbeans上重新部署项目后,mySQL数据库中的数据被清除

sql - Like 谓词在 SQL 中如何工作?

php - php 中的安全 session /cookie

PHP PDO 使用下拉菜单过滤数据

mysql - 如果列具有特定值,SQL 按列排序

php - 从 MySQL 结果自动更新列

mysql - 如何插入触发器