John Davidson

php - SQL Delete Entire Row from two tables

0 comments
Message:


Format of my tables


$sql = "CREATE TABLE tbl_topic (
topic_ID INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
topic_detail VARCHAR(1000) NOT NULL,
user_name VARCHAR(50) NOT NULL,
user_ID INT(11)
)";


$sql = "CREATE TABLE tbl_answer (
answer_ID INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
topic_ID INT(11),
answer_text VARCHAR(1000) NOT NULL,
user_name VARCHAR(50) NOT NULL,
user_ID INT(11)
)";

I'm trying to delete two rows of data where the topic_ID match from two separate tables. My Forum page has a form that posts my topic_ID through.


<td>
<form method="post" action="delete_topic.php">
<input type="hidden" name="topicID" value="<?php echo $topic["topic_ID"];?>"/>
<input type="submit" name = "submit" value = "Remove"/>
</form>
</td>



I want this information to then go to another file "delete_topic.php" which will run an SQL query to delete the row from both table tbl_answer and tbl_topic.


$top=$_POST["topicID"];
$sql = "DELETE FROM tbl_topic WHERE topic_ID= $top";

I'm unsure how to delete tbl_answer as well as tbl_topic. Any suggestions on how this is done. I have seen methods mainly using JOINS but I'm having a hard time applying it to my situation. I have not used a foreign key either if this would be another method of deleting both data at the same time.



My Answer:
"DELETE FROM table1, table2 WHERE table1.id = table2.id AND table1.id = 'value';"

Rate this post

3 of 5 based on 3641 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us