Jump to content

Delete from multiple tables..


haku87

Recommended Posts

I have four tables.. t1,t2,t3,t4

t1 has a field called "id"

t2 has a field called "quizid"

t3 has a field called "quizid"

t4 has a field called "quizid"

 

I wan to delete all entries in these four tables which has a same id as $_POST[id].

The sql statement that i wrote cannot be use.. How to write the sql statement...

 

"DELETE tblquizmanagement, tblsaqmanagement, tblmcqmanagement,tbl_essay_ans FROM tblquizmanagement AS t1,tblsaqmanagement AS t2,tblmcqmanagement AS t3,tbl_essay_ans AS t4 WHERE t1.id ='".$_GET['quizid']."' AND t2.quizid = t1.quizid AND t3.quizid = t1.quizid AND t4.quizid = t1.quizid"

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/90097-delete-from-multiple-tables/
Share on other sites

Corrected...(Wouldn't let me edit)

<?php

$tables = array('t1','t2','t3','t4');
$id = $_POST[id];
$id_field = 'id';
$i = 1;

  foreach($tables as $t) {
    if($i > 1) $id_field = 'quizid';
    $DB->query("DELETE FROM `".$t."` WHERE `".$id_field."`='$id'");
    $i++;
  }

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.