简体   繁体   English

通过在PHP中指定特定的ID来删除多个表

[英]Delete multiple table by given a specific id in php

Good day. 美好的一天。 I'm new to php and now wanted to learn on how to delete 3 tables from android to php by given a specific ID. 我是php的新手,现在想学习如何通过给定特定ID将3个表从android删除到php。

Table Info (id, name) Table Good (id, sun, tf) Table Bad (id, moon, ts) 表信息(id,名称)表良(id,sun,tf)表坏(id,月亮,ts)

    <?php 
     //Getting Id
     $id = $_GET['id'];

     //Importing database
     require_once('dbConnect.php');

     //Creating sql query
     $sql = "DELETE FROM info WHERE id=$id;";
     $sql ="DELETE FROM Good WHERE tf=$id;";
     $sql ="DELETE FROM Bad WHERE ts=$id;";

     //Deleting record in database 
     if(mysqli_query($con,$sql)){
     echo 'Employee Deleted Successfully';
     }else{
     echo 'Could Not Delete Employee Try Again';
     }

     //closing connection 
     mysqli_close($con);
?>

Error 错误

在此处输入图片说明

Latest code 最新代码

<?php 
 //Getting Id
 $mysqli->multi_query(" Many SQL queries ; ");
 $id = (int)$_GET['id'];

 //Importing database
 require_once('dbConnect.php');

 //Creating sql query
 $sql  = "DELETE FROM informaation WHERE id=$id;";
 $sql .= "DELETE FROM work_force WHERE twf=$id;";
 $sql .= "DELETE FROM work_details WHERE ts_id=$id;";

 //Deleting record in database 
 if(multi_query($con,$sql)){
 echo ' Deleted Successfully';
 }else{
 echo 'Could Not Delete . Try Again.' . mysqli_error($con);
 }
 ?>

You need to use a multi-query because you are presently overwriting your first 2 queries. 您需要使用一个多查询,因为当前您正在覆盖前两个查询。

I modified your code to concatenate the query statements and the proper function for it, including error checking. 我修改了您的代码以连接查询语句及其适当的功能,包括错误检查。 Plus, assuming you did successfully connect using the mysqli_ library. 另外,假设您确实使用mysqli_库成功连接。

Your require_once('dbConnect.php'); 您的require_once('dbConnect.php');

should contain this below and replace the credentials below with your own. 应该在下面包含此凭证,并用您自己的凭证替换以下凭证。

 $con = mysqli_connect("yourhost", "my_user", "my_password", "my_db");

Then do: 然后做:

require_once('dbConnect.php');

 $id = (int)$_GET['id'];

 $sql  = "DELETE FROM info WHERE id=$id;";
 $sql .= "DELETE FROM Good WHERE tf=$id;";
 $sql .= "DELETE FROM Bad WHERE ts=$id;";

 //Deleting record in database 
 if(mysqli_multi_query($con,$sql)){
 echo 'Employee Deleted Successfully';
 }else{
 echo 'Could Not Delete Employee Try Again.' . mysqli_error($con);
 }

Reference: 参考:

Example from the manual: 手册中的示例:

 
 
 
  
  <?php // WORKING CODE: $mysqli->multi_query(" Many SQL queries ; "); // OK while ($mysqli->next_result()) {;} // flush multi_queries $mysqli->query(" SQL statement #1 ; ") // now executed! $mysqli->query(" SQL statement #2 ; ") // now executed! $mysqli->query(" SQL statement #3 ; ") // now executed! $mysqli->query(" SQL statement #4 ; ") // now executed! ?>
 
  

Plus, make sure you do have a value for your GET array. 另外,请确保您确实拥有GET数组的值。

If it is indeed an integer, replace it with: 如果确实是整数,则将其替换为:

$id = (int)$_GET['id'];

Your present code is open to SQL injection . 您当前的代码可以进行SQL注入 Use mysqli_* with prepared statements , or PDO with prepared statements . mysqli_*与预处理语句一起使用 ,或将PDO预处理语句一起使用

从i.info中删除g.good,b.bad,其中i.id = $ id AND g.id = $ id AND b.id = $ id

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

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