Jump to content

Delete records from mutiple tables + unlink


bob_the _builder

Recommended Posts

Hi,

I have 3 tables:

[code]CREATE TABLE product_headings (
  heading_id int(10) NOT NULL auto_increment,
  product varchar(100) NOT NULL default '',
  PRIMARY KEY (heading_id)
) TYPE=MyISAM;

###

CREATE TABLE products (
  product_id int(10) NOT NULL auto_increment,
  filter int(10) NOT NULL default '',
  product_name varchar(100) NOT NULL default '',
  description text NOT NULL,
  current int(10) NOT NULL default '',
  PRIMARY KEY (product_id)
) TYPE=MyISAM;

###

CREATE TABLE product_images (
  photo_id int(10) NOT NULL auto_increment,
  product_id int(10) NOT NULL default '0',
  photo_filename varchar(25) NOT NULL default '0',
  PRIMARY KEY (photo_id)
) TYPE=MyISAM;[/code]

Im not sure how to create a relationship between the tables and images, If somone chooses to delete a "product_heading" have it also delete the related "products" from the product table and also unlink the images related to the products, whic is where I am stuck.

The following code is what I am using to delete a product from the "products" table and unlink all the images related to that product:

[code=php:0]if (array_key_exists("confirm",$_REQUEST)) {

$sql = mysql_query("SELECT photo_filename FROM product_images WHERE product_id='".NumValidate($_GET['product_id'])."'");
while ($row = @mysql_fetch_array($sql)) {
  @unlink($images_dir . '/' . $row[0]);
  @unlink($images_dir . '/tb_' . $row[0]);
}
@mysql_query("DELETE FROM product_images WHERE product_id = '".NumValidate($_GET['product_id'])."'");
@mysql_query("DELETE FROM products WHERE product_id = '".NumValidate($_GET['product_id'])."'");

echo '<br /><center><b>Record was successfully deleted</b></center>';
}else{
echo '<br /><center><b>Do you really want to delete this record?</b></center>';
echo '<br /><br /><center><a href="../index.php?action=del_products&product_id='.$_GET['product_id'].'&del_all&confirm=y">Yes</a> - <a href="#" onClick="history.go(-1)">No</a></center>';
}[/code]

Thanks.

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.