Jump to content

Delete loop not working


GalaxyTramp

Recommended Posts

I am trying to delete records across 3 tables but my code does not work.

 



$query = ("SELECT `propertyref` FROM `feed_property` WHERE `status` = 'Sold' ")or die(mysql_error());
   $result = mysql_query($query);
   
if (isset($result)) {   
     while ($row = mysql_fetch_array($result)):
     $ref1 = mysql_real_escape_string($row['propertyref']);
  
    
$query1 = ("DELETE FROM `feed_property`, `feed_images`, `feed_characteristics`\n"
    . "USING `feed_property` INNER JOIN `feed_images` INNER JOIN `feed_characteristics`\n"
    . "WHERE feed_property.propertyref = '$ref1'\n"
    . " AND feed_images.propertyref = feed_property.propertyref\n"
    . " AND feed_characteristics.propertyref = feed_property.propertyref; ")or die(mysql_error());
    
    
    
    

echo $query1;
endwhile;


    
if(!mysql_query($query1)){
  echo '<h1 style="color: red;">Error</h1><p>', mysql_error(), '</p>';
}


else
{

  echo '<h1 style="color: red;">Properties have been removed from the database</h1>';
}
}

 

 

Query1 echos as:

 

DELETE FROM `feed_property`, `feed_images`, `feed_characteristics` USING `feed_property` INNER JOIN `feed_images` INNER JOIN `feed_characteristics` WHERE feed_property.propertyref = 'abc1' AND feed_images.propertyref = feed_property.propertyref AND feed_characteristics.propertyref = feed_property.propertyref; 
DELETE FROM `feed_property`, `feed_images`, `feed_characteristics` USING `feed_property` INNER JOIN `feed_images` INNER JOIN `feed_characteristics` WHERE feed_property.propertyref = 'abc2' AND feed_images.propertyref = feed_property.propertyref AND feed_characteristics.propertyref = feed_property.propertyref;

 

The query completes without errors but does not delete the entries in the DB. If I run the query singly in PHP My Admin it functions correctly.

 

thanks for your help

 

 

 

 

Link to comment
Share on other sites

As per your code, you're not calling mysql_query properly and you're not calling it in the right spot. Since you want it to execute for each loop, it should be more like this:

 

$query = ("SELECT `propertyref` FROM `feed_property` WHERE `status` = 'Sold' ")or die(mysql_error());
$result = mysql_query($query);

if (isset($result)) {
    while ($row = mysql_fetch_array($result)) {
        $ref1 = mysql_real_escape_string($row['propertyref']);


        $query1 = "DELETE FROM `feed_property`, `feed_images`, `feed_characteristics`\n"
            . "USING `feed_property` INNER JOIN `feed_images` INNER JOIN `feed_characteristics`\n"
            . "WHERE feed_property.propertyref = '$ref1'\n"
            . " AND feed_images.propertyref = feed_property.propertyref\n"
            . " AND feed_characteristics.propertyref = feed_property.propertyref; ";


        if(!mysql_query($query1)){
            echo '<h1 style="color: red;">Error</h1><p>', mysql_error(), '</p>';
        }
        else {
            echo '<h1 style="color: red;">Properties have been removed from the database</h1>';
        }


        echo $query1;
    }

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.