2

I have a database with many tables. On side 1, I have a button that passes me to side 2. On side 2, I want the code to remove every row in every named table that has a specific pid that I have stored in a variable. I have a code on side 2 but it doesn't do anything.

Code on side 2:

$pid3 = $_POST['pid2'];

$del = "DELETE FROM answer_det WHERE pid='$pid3'";
$del .= "DELETE FROM project WHERE pid='$pid3'";
$del .= "DELETE FROM question WHERE pid='$pid3'";
$del .= "DELETE FROM respondent WHERE pid='$pid3'";
$del .= "DELETE FROM result WHERE pid='$pid3'";
$del .= "DELETE FROM users WHERE pid='$pid3'";

$run = mysqli_query($mysqli,$del);

if($run)
{
   echo "<h1>Project is deleted!</h1>";
}
FirstOne
  • 5,165
  • 5
  • 24
  • 44
Max
  • 377
  • 1
  • 9
  • Better than sanitizing, use prepared statements with placeholders. https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Andy Lester Dec 23 '15 at 18:37

6 Answers6

4

Use mysqli_multi_query

$pid3 = $_POST['pid2'];

$del = "DELETE FROM answer_det WHERE pid='$pid3';";
$del .= "DELETE FROM project WHERE pid='$pid3';";
$del .= "DELETE FROM question WHERE pid='$pid3';";
$del .= "DELETE FROM respondent WHERE pid='$pid3';";
$del .= "DELETE FROM result WHERE pid='$pid3';";
$del .= "DELETE FROM users WHERE pid='$pid3';";

$run = mysqli_multi_query($mysqli,$del);

if($run)
{
   echo "<h1>Project is deleted!</h1>";
}
Nana Partykar
  • 10,175
  • 8
  • 43
  • 73
1

You might consider altering your mysql tables where pid exists to have that field as a foreign key which cascades on delete. Something like

ALTER TABLE `answer_det` 
ADD FOREIGN KEY (pid) 
REFERENCES table_where_pid_is_the_primary_key (pid)
ON DELETE CASCADE;

replacing answer_det with the other table names to create keys there too. If you don't have access to alter the tables then likely your queries are failing because you are missing semicolons to separate them:

$del = "DELETE FROM answer_det WHERE pid='$pid3';";
$del .= "DELETE FROM project WHERE pid='$pid3';";
$del .= "DELETE FROM question WHERE pid='$pid3';";
$del .= "DELETE FROM respondent WHERE pid='$pid3';";
$del .= "DELETE FROM result WHERE pid='$pid3';";
$del .= "DELETE FROM users WHERE pid='$pid3';";
wogsland
  • 7,351
  • 16
  • 46
  • 79
0

Or, you could just run one query, with joins, something like this:

$pid3 = $_POST['pid2'];

$del = "DELETE a,b,c,d,e,f FROM answer_det a "
$del .= "JOIN project b on b.pid=a.pid "
$del .= "JOIN question c  on c.pid=a.pid "
$del .= "JOIN respondent d on d.pid=a.pid "
$del .= "JOIN result e on e.pid=a.pid "
$del .= "JOIN users f on f.pid=a.pid "
$del .= "WHERE a.pid='$pid3'";

$run = mysqli_query($mysqli,$del);

if($run)
{
   echo "<h1>Project is deleted!</h1>";

}

How to delete from multiple tables in MySQL?

Community
  • 1
  • 1
Andrew
  • 13,934
  • 8
  • 78
  • 93
0

simplify your query as given below

$pid3 = $_POST['pid2'];    
 $del= DELETE FROM answer_det 't1',project 't2',question 't3' USING t1, t2,t3 WHERE t3.pid = t2.pid and t1.pid = t2.pid AND t1.pid =  $pid3
$run = mysqli_query($mysqli,$del);
if($run)
{
echo "<h1>Project is deleted!</h1>";
}

refer. delete rows from multiple tables

Community
  • 1
  • 1
Vigneswaran S
  • 1,942
  • 1
  • 17
  • 32
0

There's a good solution:

DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;

Community
  • 1
  • 1
-1

You were missing the semi-colon at the end of the query. You need to put ';' after each query in order to execute multiple queries together.

Try this:

$pid3="1";

$del = "DELETE FROM answer_det WHERE pid='$pid3';";
$del .= "DELETE FROM project WHERE pid='$pid3';";
$del .= "DELETE FROM question WHERE pid='$pid3';";
$del .= "DELETE FROM respondent WHERE pid='$pid3';";
$del .= "DELETE FROM result WHERE pid='$pid3';";
$del .= "DELETE FROM users WHERE pid='$pid3';";

echo $del
  • 1
    Even though that's important, that alone won't solve the problem without [mysqli_multi_query](http://php.net/mysqli_multi_query). – FirstOne Dec 23 '15 at 17:57