简体   繁体   中英

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.

Table Info (id, name) Table Good (id, sun, tf) Table Bad (id, moon, 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.

Your 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.

If it is indeed an integer, replace it with:

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

Your present code is open to SQL injection . Use mysqli_* with prepared statements , or PDO with prepared statements .

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

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