Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have three Tables in database.

table 1

projects

PK ProjectID

table 2

students

PK RegNo FK ProjectID table 3

progress FK RegNo

Now the thing i want to perform a delete operation when i delete record from project it should be deleted from students, as students primary key is also present as foreign key progress table, so it should also delete RegNo from progress table. How i can achieve this as the best possible way. Thanks in advance.

What I have tried:

PHP
$query = "

        DELETE students, progress from students inner join progress on progress.RegNo=students.RegNo where students.ProjectID='$id';
        DELETE FROM projects where projects.ProjectID='$id'; 
";

//$conn->exec($query);
$stmt = $conn->prepare($query); $stmt->execute();


it gives foreign key constraint violation
Posted
Updated 11-Sep-20 23:02pm
v2

Option 1:
Set the foreign key to cascade on delete:
MySQL ON DELETE CASCADE: Deleting Data From Multiple Tables[^]

Option 2:
Delete the data from all related tables manually:
SQL
DELETE FROM progress FROM progress INNER JOIN students ON progress.RegNo = students.RegNo WHERE students.ProjectID = :id;
DELETE FROM students WHERE students.ProjectID = :id;
DELETE FROM projects WHERE projects.ProjectID = :id;

NB: Use parameters to pass values to the query, rather than inserting the values directly into the string. If you don't, you can make your code vulnerable to SQL Injection.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
 
Share this answer
 
PHP
$query = "
        DELETE students, progress from students inner join progress on progress.RegNo=students.RegNo where students.ProjectID='$id';
        DELETE FROM projects where projects.ProjectID='$id'; 
";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900