haku87 Posted February 8, 2008 Share Posted February 8, 2008 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 More sharing options...
Caesar Posted February 8, 2008 Share Posted February 8, 2008 <?php $tables = array('t1','t2','t3','t4'); $id = $_POST[id]; $id_field = 'id'; $i = 1; foreach($tables as $t) { if($i > 1)$id = 'quizid'; $DB->query("DELETE FROM `".$t."` WHERE `".$id_field."`='$id'"); $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/90097-delete-from-multiple-tables/#findComment-461960 Share on other sites More sharing options...
Caesar Posted February 8, 2008 Share Posted February 8, 2008 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++; } ?> Link to comment https://forums.phpfreaks.com/topic/90097-delete-from-multiple-tables/#findComment-461967 Share on other sites More sharing options...
haku87 Posted February 9, 2008 Author Share Posted February 9, 2008 Is there a way to delete all the four table with quiz id = 3 in one sql query? Link to comment https://forums.phpfreaks.com/topic/90097-delete-from-multiple-tables/#findComment-462442 Share on other sites More sharing options...
shedokan Posted February 9, 2008 Share Posted February 9, 2008 maybe try: DELETE FROM `t1` WHERE `id'='3' DELETE FROM `t2`,`t3`,`t4` WHERE `quizid'='3' Link to comment https://forums.phpfreaks.com/topic/90097-delete-from-multiple-tables/#findComment-462479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.