Jump to content

DELETING FROM 2 TABLES


jwo1985

Recommended Posts

I've got 2 tables: Album and Song, the tables are linked by an albumid field.  Once an albumid is entered on another webpage, the intention of this page is to actually do the deadly deed and delete the record from both tables.  The only trouble is that it doesn't work, can anyone suggest a solution?

 

$albumid = $_POST[albumid];

$sql = "DELETE FROM album, song USING album, song WHERE albumid=$albumid";

$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
else {
echo "Record Deleted.";

 

 

Link to comment
Share on other sites

DELETE album, song
FROM album
     INNER JOIN song ON album.albumid = song.albumid 
WHERE album.albumid = $albumid

 

Realised that my tables were actually called jwo1_album and jwo1_song so tried:

 

$sql = "DELETE jwo1_album, jwo1_song
FROM jwo1_album
     INNER JOIN jwo_song ON jwo1_album.albumid = jwo1_song.albumid 
WHERE jwo1_album.albumid = $albumid";

 

but it didn't work... any suggestions why this may be?

Link to comment
Share on other sites

I tried the LEFT JOIN and this is the error message that I get...

 

Invalid query: You have an error in your SQL syntax near 'jwo1_album, jwo1_song FROM jwo1_album LEFT JOIN jwo1_song O' at line 1

 

The code is...

 

<?php
$conn = mysql_connect("*****", "****", "****");
mysql_select_db("******", $conn);
//assign variables
$albumid = $_POST[albumid];
$sql = "DELETE jwo1_album, jwo1_song
FROM jwo1_album
    LEFT JOIN jwo1_song ON jwo1_album.albumid = jwo1_song.albumid 
WHERE jwo1_album.albumid = $albumid";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
else {
echo "Record Deleted.";
}
?>

 

I made sure that the albumid was moving across from the other webpage, where the user selects which album to delete, by just printing the value of $albumid to the screen. So there doesn't appear to be a problem with that variable, but there is something else stopping it from working and I can't see it!

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.