Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2012
    Posts
    14

    Unanswered: Query for deleting records from multiple tables in db

    Hi,
    I am having difficulty deleting records from multiple tables within mysql using php. I have 2 tables where the records will need to be deleted from - item and links.

    Below is the code we are using:

    Code:
    <?php 
    	if(isset($_GET['item_id'])) {
    		
    		
    		$outcome = mysql_query("
    		
    		SELECT item_id, item_name, item_description, item_price, item_image
    		FROM item
    		LEFT JOIN links ON item.item_id = links.item_id
    		WHERE item.item_id = ".$_GET['item_id']);
    		
    
    			$row = mysql_fetch_array($outcome);
    		
    		
    	
    	if(isset($_GET['no'])) {
    		header('Location: ourworkadmin.php');
    	}
    	
    	if(isset($_GET['yes'])) {
    		unlink("../images/ ".$row['item_image']);
    		
    		$query = ("DELETE item, links
    		LEFT JOIN links
    		ON item.item_id = links.item_id 
    		WHERE item.item_id = ".$_GET['item_id']);
    		$delete = mysql_query($query);
    		
    			
    		header('Location: ourworkadmin.php');
    	}
    	}
    ?>
    This is the form:

    <p>Are you sure you wish to delete <strong><?php echo $row['item_name']; ?></strong>?</p>

    <form action="item-delete.php" method="get" class="admin_form">
    <input type="hidden" name="item_id" value="<?php echo $_GET['item_id'];?>" />
    <input name="no" type="submit" value="No" />
    <input name="yes" type="submit" value="Yes" />
    </form>

    Any help would be appreciated!

  2. #2
    Join Date
    May 2008
    Posts
    277
    Quote Originally Posted by gwg View Post
    PHP Code:
    $query = ("DELETE item, links
            LEFT JOIN links
            ON item.item_id = links.item_id 
            WHERE item.item_id = "
    .$_GET['item_id']); 
    This is not how you delete from multiple tables. Just do multiple deletes, one for each table.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •