Reputation:
I have made a table with rows of information in it, next to the rows I have included a 'delete' button, which I was hoping would delete that specific row of information from the SQL. However, I keep getting errors that it failed to connect as I do not know how to give the row a specific number so the SQL knows which row to delete. Here is the code for the table:
However, it does not work, the page doesnt refresh or delete from SQL.
I hope someone can help me sort out my issue. Thank you,
Upvotes: 0
Views: 111
Reputation: 1582
Change this part of your code:
// sql to delete a record
$sql = "DELETE FROM `system` WHERE system.id=5;
DELETE FROM `booking` WHERE booking.id=5;
DELETE FROM `details` WHERE details.id=5;";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
to:
$sqls = array("DELETE FROM `system` WHERE system.id=5;",
"DELETE FROM `booking` WHERE booking.id=5;",
"DELETE FROM `details` WHERE details.id=5;");
foreach ($sqls as $sql) {
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
}
But i really think the best approach is to modify your database schema and enable cascade on delete using foreign keys. (https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html)
Upvotes: 1