I have some code that works fantastically that looks like this. You can assume safely that the DB connection is successfully made previously and there is no DB communication issues here:
if($value['caseKey'] === "Case01") {
try {
$stmt = $conn->prepare("SELECT * FROM tbl_Lodged, tbl_Search, tbl_Notes, tbl_Files,
tbl_Case01_s1, tbl_Case01_s2, tbl_Case01_s3, tbl_Case01_s4, tbl_Case01_s5, tbl_Case01_s6, tbl_Case01_s7
WHERE tbl_Lodged.uaID = tbl_Search.uaID AND tbl_Search.uaID = tbl_Notes.uaID
AND tbl_Notes.uaID = tbl_Files.uaID AND tbl_Files.uaID = tbl_Case01_s1.uaID
AND tbl_Case01_s1.uaID = tbl_Case01_s2.uaID AND tbl_Case01_s2.uaID = tbl_Case01_s3.uaID
AND tbl_Case01_s3.uaID = tbl_Case01_s4.uaID AND tbl_Case01_s4.uaID = tbl_Case01_s5.uaID
AND tbl_Case01_s5.uaID = tbl_Case01_s6.uaID AND tbl_Case01_s6.uaID = tbl_Case01_s7.uaID
AND tbl_Lodged.uaID = :uaid");
$stmt->bindValue(':uaid', $value['uaID']);
$stmt->execute();
$caseRes = $stmt->fetchAll();
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
} else if($value['caseKey'] === "Case05") {
try {
$stmt = $conn->prepare("SELECT * FROM tbl_Lodged, tbl_Search, tbl_Notes, tbl_Files,
tbl_Case05_s1, tbl_Case05_s2, tbl_Case05_s3, tbl_Case05_s4, tbl_Case05_s5
WHERE tbl_Lodged.uaID = tbl_Search.uaID AND tbl_Search.uaID = tbl_Notes.uaID
AND tbl_Notes.uaID = tbl_Files.uaID AND tbl_Files.uaID = tbl_Case05_s1.uaID
AND tbl_Case05_s1.uaID = tbl_Case05_s2.uaID AND tbl_Case05_s2.uaID = tbl_Case05_s3.uaID
AND tbl_Case05_s3.uaID = tbl_Case05_s4.uaID AND tbl_Case05_s4.uaID = tbl_Case05_s5.uaID
AND tbl_Lodged.uaID = :uaid");
$stmt->bindValue(':uaid', $value['uaID']);
$stmt->execute();
$caseRes = $stmt->fetchAll();
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
} else ....... etc etc
That working fine for selecting everything relative to the tables that contain data for the matching caseKey with the corresponding uaID, I want to perform much the same thing but instead of SELECT
I would like to perform DELETE
and so I tried this:
if($value['caseKey'] === "Case01") {
try {
$stmt = $conn->prepare("DELETE * FROM tbl_Lodged, tbl_Search, tbl_Notes, tbl_Files,
tbl_Case01_s1, tbl_Case01_s2, tbl_Case01_s3, tbl_Case01_s4, tbl_Case01_s5, tbl_Case01_s6, tbl_Case01_s7
WHERE tbl_Lodged.uaID = tbl_Search.uaID AND tbl_Search.uaID = tbl_Notes.uaID
AND tbl_Notes.uaID = tbl_Files.uaID AND tbl_Files.uaID = tbl_Case01_s1.uaID
AND tbl_Case01_s1.uaID = tbl_Case01_s2.uaID AND tbl_Case01_s2.uaID = tbl_Case01_s3.uaID
AND tbl_Case01_s3.uaID = tbl_Case01_s4.uaID AND tbl_Case01_s4.uaID = tbl_Case01_s5.uaID
AND tbl_Case01_s5.uaID = tbl_Case01_s6.uaID AND tbl_Case01_s6.uaID = tbl_Case01_s7.uaID
AND tbl_Lodged.uaID = :uaid");
$stmt->bindValue(':uaid', $value['uaID']);
$stmt->execute();
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
} else if($value['caseKey'] === "Case05") {
try {
$stmt = $conn->prepare("DELETE * FROM tbl_Lodged, tbl_Search, tbl_Notes, tbl_Files,
tbl_Case05_s1, tbl_Case05_s2, tbl_Case05_s3, tbl_Case05_s4, tbl_Case05_s5
WHERE tbl_Lodged.uaID = tbl_Search.uaID AND tbl_Search.uaID = tbl_Notes.uaID
AND tbl_Notes.uaID = tbl_Files.uaID AND tbl_Files.uaID = tbl_Case05_s1.uaID
AND tbl_Case05_s1.uaID = tbl_Case05_s2.uaID AND tbl_Case05_s2.uaID = tbl_Case05_s3.uaID
AND tbl_Case05_s3.uaID = tbl_Case05_s4.uaID AND tbl_Case05_s4.uaID = tbl_Case05_s5.uaID
AND tbl_Lodged.uaID = :uaid");
$stmt->bindValue(':uaid', $value['uaID']);
$stmt->execute();
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
} else ....... etc etc
I thought by changing the SELECT
to DELETE
and removing the $caseRes = $stmt->fetchAll();
line, I'd have a fairly good solution. Turns out I was wrong on that one, sadly.
But you can see what I am trying to do though. I did some research and discovered joins, both inner and outer versions. I don't get them at all, they don't make sense in my head because I basically revert to being 15 and remembering my woodwork teacher talking about inner and outer joins on assembly of a wooden box. I tend to see things in a very visual representation and the code above that I have quoted makes sense to me because it's simply filtering by caseKey to the next stage whereby all the tables with a common uaID that is being called, are being acting upon, in a way that's a bit like pass the parcel - or the uaID in this case.
Not liking/understanding joins I figured I would have to do each relative table manually. Painful, lots of code and error potential. I believe this might have been what's called a code smell?
More reading and I discovered something called Foreign Keys in mySQL. It appears maybe I can just have this in my php:
try {
$stmt = $conn->prepare("DELETE * FROM tbl_Lodged WHERE uaID = :uaid");
$stmt->bindValue(':uaid', $value['uaID']);
$stmt->execute();
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
And then magically anything that has the particular uaID
will be eliminated.
Is this true, or am I hoping for too much?
If it is true, how can I set this up using phpmyadmin version 3.4.11.1deb2+deb7u1? I've read on how to do this but what I read is quite different to what I see in my phpmyadmin and does not correspond in any way close enough to use. Should I give up on Foreign Keys and re-try understanding joins for this operation?
the uaID
is set as int(11), auto increment, Primary Key in the tbl_Lodged table. It is set the same within tbl_Search table. It is a key in the other relevant tables but is not the primary key.
Thanks in advance!
PS please keep in mind I am not a code ninja. I'm trying hard though..!
PPS - the database is using the InnoDB engine.
Thanks to Ronald (below) I should clarify I am wanting to set the FK functionality up using phpmyadmin but I don't want to resort to having to use phpmyadmin to actually delete anything. The point of setting up the FK's if it'll work is so that php can do it automatically via a cron job. Phpmyadmin is only my tool to set up and make changes to the database, not run or manage it. I cannot recreate tables that already exist but I can hopefully modify existing ones to fit what is needed to make the delete magic work.
UPDATE Does anyone know how to remove a question from here??