I wanna create a button which deletes all the records that needs to be deleted. I have three tables in my database person, address and cv... address_id has a relation with person_address in person table and cv_id has a relation with person_cv in person table. When the delete button is pressed everything connected to the person should be deleted. I tried lots of things like:
echo "<td><a href='delete.php?id=<?" . $row['id'] . "'>delete</a></td>";
With a external delete.php but it's not working at all... link in the link the tutorial I used for delete.php
My detail page where the delete button should be in after CV:
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT person_firstname, person_lastname,
person_email, person_phonenumber,
address_street,address_housenumber,
address_city,address_state,address_zipcode, cv_path
FROM person
inner join address on address.address_id = person.person_address
inner join cv on cv.cv_id = person.person_cv";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border=1>
<tr>
<th>Voornaam</th>
<th>Achternaam</th>
<th>Straat</th>
<th>Huisnummer</th>
<th>Postcode</th>
<th>Stad</th>
<th>Provincie</th>
<th>Email</th>
<th>Mobiel</th>
<th>CV</th>
</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["person_firstname"] . "</td>";
echo "<td>" . $row["person_lastname"] . "</td>";
echo "<td>" . $row["address_street"] . "</td>";
echo "<td>" . $row["address_housenumber"] . "</td>";
echo "<td>" . $row["address_zipcode"] . "</td>";
echo "<td>" . $row["address_city"] . "</td>";
echo "<td>" . $row["address_state"] . "</td>";
echo "<td>" . $row["person_email"] . "</td>";
echo "<td>" . $row["person_phonenumber"] . "</td>";
echo "<td><a href='http://localhost:8080/website/" . $row['cv_path'] . "'>cv file</a></td>";
echo "</tr>";
}
}
else {
echo "Er is niks in het database gevonden";
}
$conn->close();
?>
Delete.php:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="usbw"; // Mysql password
$db_name="persons"; // Database name
$tbl_name="person"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['person_id'];
// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE person_id='$id'";
$result=mysql_query($sql);
// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='admin.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>