Log in

View Full Version : PHP/MySQL - Delete from two tables and server files



megs1328
05-16-2009, 07:01 PM
Currently, my site is set up so you have to delete all the music and then the band if you want both gone. I'd like to make it so that if a band is deleted, all their music is as well. I am using Dreamweaver CS3

To delete the music only I have an unlink:


if ((isset($_POST['musicID'])) && ($_POST['musicID'] != "")) {
$image_path = '../includes/audio/';
if (isset($_POST['trackdirectory']) && file_exists($image_path.$_POST['trackdirectory'])) {
unlink($image_path.$_POST['trackdirectory']);
}

$deleteSQL = sprintf("DELETE FROM music WHERE musicID=%s",
GetSQLValueString($_POST['musicID'], "int"));

mysql_select_db($database_everyscene, $everyscene);
$Result1 = mysql_query($deleteSQL, $everyscene) or die(mysql_error());

$deleteGoTo = "viewbands.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}


My delete artist page currently is:


if ((isset($_POST['artistID'])) && ($_POST['artistID'] != "")) {
$deleteSQL = sprintf("DELETE FROM artists WHERE artistID=%s",
GetSQLValueString($_POST['artistID'], "int"));

mysql_select_db($database_everyscene, $everyscene);
$Result1 = mysql_query($deleteSQL, $everyscene) or die(mysql_error());

$deleteGoTo = "viewbands.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}



Is there a way to add the delete music from a band into the delete a band page? My music table is currently set up as: musicID, track_directory (which links to the server file), artistID (which links to the artist table). Thank you

megs1328
05-27-2009, 03:11 AM
Does any of that make sense?

forum_amnesiac
05-27-2009, 08:08 AM
Unless I am misunderstanding you have posted here is a simple way to delete all the artists music if the artist is deleted.


if ((isset($_POST['artistID'])) && ($_POST['artistID'] != "")) {

$artist=$POST['artistID'];

$delartist = "DELETE FROM artists WHERE artistID=$artist";
$delmusic = "DELETE FROM music WHERE artistID=$artist";

$Result1 = mysql_query($delartist) or die(mysql_error());
$Result2 = mysql_query($delmusic) or die(mysql_error());

You obviously need to open your database before this code, I haven't tested it but it should work.

There are other ways of doing this, by joining the tables, but that is a bit more complicated.