I would like to know if anyone can help me with the following problem. The website I am trying to build contains a page that allows people to edit information that has already been entered into a database. I have decided to edit some information using an UPDATE SQL query, but for the process I am working on, it seems like an INSERT followed by a DELETE will do the job. I am able to add the new information with an INSERT query, but I cannot delete the old information.
Here is an explanation of the code. $bid is passed on from the previous page. It is used to delete entries from the BandMember table in the first section of code. This part works fine. After that, I am trying to use $bid to retrieve the relevant mid from the BandMember table. Once I have the correct mid, the row corresponding to that mid can be deleted from the Member table.
The third section of the code works fine.
// SECTION ONE
$ok = mysql_query("DELETE FROM BandMember WHERE bid='$bid'");
if (!$ok) {
die("<p>Error removing band from BandMember table:" .
mysql_error() . "</p>");
}
/* SECTION TWO, THIS PART DOES NOT WORK,
TRYING TO DELETE OLD ENTRIES FROM MEMBER TABLE */
$ids = @mysql_query("SELECT BandMember.mid FROM BandMember " .
"WHERE bid='$bid'");
while ($id = mysql_fetch_array($ids)) {
$mid = $id['mid'];
$ok = mysql_query("DELETE FROM Member WHERE Member.mid='$mid'");
if (!$ok) {
die("<p>Error removing members from Member table: " . mysql_error() . "</p>");
}
}
// SECTION THREE, insert info into Member and BandMember tables
for($i = 0; $i < count($newMemberNames); $i++) {
$name = $newMemberNames[$i];
$instrument = $newInstruments[$i];
$insertMember = "INSERT INTO Member (memberName, instrument) " .
"VALUES ('$name', '$instrument')";
$queryInsertMember = mysql_query($insertMember);
if(!$queryInsertMember) {
print mysql_error();
exit();
}
$insertBandMember = "INSERT INTO BandMember (bid, mid) " .
"VALUES ('$bid', LAST_INSERT_ID())";
$queryInsertBandMember = mysql_query($insertBandMember);
if(!$queryInsertBandMember) {
print mysql_error();
exit();
}
}
Thank you for any help you can offer.