I want to make an check for my application to see if my table in MySQL contains a foreign key. If so, I want to remove the option to delete the table containing the foreign key. Here is my code:
<html>
<head>
<link href="css/stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<?php
require_once 'autoloader.php';
// cursusManager is verantwoordelijk voor alle person handeling op
// de database.
$cursusManager = new cursusmanager();
// Aanname dat delete de enige action is op dit scherm
if(isset($_GET["action"]) && $_GET["action"] == "delete") {
// Aanname dat het id netjes word meegenomen!
$id = $_GET["id"];
$cursusManager->delete($id);
}
// Haal alle cursussen uit de database om deze verderop te laten zien.
$cursussen = $cursusManager->getAll();
?>
<img src="images/logo.png" id="logo" />
<h2>Cursussen</h2>
<!-- simpel gridje, 123 hatsaflats -->
<div class="grid">
<div class="gridheader">Naam</div>
<div class="gridheader">Aantal jaar</div>
<div class="gridheader">Niveau</div>
<div class="gridheader gridaction"></div>
<?php
// loop door alle personen heen
// (in code bovenaan uit database gehaald)
foreach($cursussen as $cursus) {
echo "<div class='gridcontent'>$cursus->naam</div>";
echo "<div class='gridcontent'>$cursus->aantaljaar</div>";
echo "<div class='gridcontent'>$cursus->niveau</div>";
echo "<div class='gridcontent gridaction'><a href='index.php?action=delete&id=$cursus->id'>delete</a></div>";
}
?>
</div>
</body>
This is the code of my php manager:
<?php
/* cursusmanager inherits van mastermanager. Op dit moment bevat de mastermanager
* alleen nog een database verbinding.
*/
class cursusmanager extends mastermanager {
public function getById($aId) {
$statement = $this->connection->prepare("SELECT * FROM cursus WHERE id = ?");
$statement->bindValue(1, $aId);
$statement->execute();
/* fetchobject haalt het eerste record op uit het statement. PHP
* maakt hier zelf een object van, dus het model person.php wordt
* hier nu niet gebruikt. fetchobject geeft null als er geen records
* zijn
*/
return $statement->fetchObject();
}
public function getAll() {
$statement = $this->connection->prepare("SELECT * FROM cursus");
$statement->execute();
/* fetchAll(PDO::FETCH_OBJ) haalt ALLE records op uit het statement. PHP
* maakt hier zelf objecten van, fetchAll(PDO::FETCH_OBJ) geeft een lege array als
* er geen records zijn
*/
return $statement->fetchAll(PDO::FETCH_OBJ);
}
public function delete($aId) {
throw new Exception("DIT IS DUS NOG NIET GEMAAKT!");
}
}
?>
You need to connect to the Information scheme and you can find all the information about the primary key
and foreign keys
in this table
SELECT * FROM information_schema.TABLE_CONSTRAINTS T;
you need to be a ROOT
user to access the information_schema.
USING this table you can find the table, db and whether it has foreign key.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.