In the top of my admin PHP (admin.php), I have loaded data from "Sport" table in "DogSport" MySQL database. After clicking "Edit" link of a particular record, I can edit, delete that record on "editform2.php". Even on that page, I can also insert a new record.
Problem: Deleting first and second records do not work! Deleting all following records work fine. This problem only with deleting functionality.
I have uploaded necessary files, you can also use to check it including database .sql script. Your help is appreciated.
https://www.dropbox.com/s/wn40u93oa1sxcph/SO.rar?dl=0
Code segment - admin.php
include("dbconnect.inc.php");
//Retrieving Data
$query1 = "SELECT * FROM Sport ORDER BY SportId ASC";
$result2 = mysqli_query($con, $query1);
$rows = mysqli_num_rows($result2);
$i = 0;
if ($rows == 0)
echo "<br/>There are no records";
else {
echo "<table id='sport'>";
echo "<tr><th>Sport Id</th><th>Sport Name</th><th>Description</th></tr>";
while ($row = mysqli_fetch_array($result2)) {
echo "<tr><td>" . $row["SportId"] . "</td><td>" . $row["SportName"] . "</td><td>" . $row["Description"] .
"</td><td> <a href='editform2.php?id=" . $row["SportId"] . "' target='_blank'>Edit</a></td></tr>";
$i++;
}
echo "</table>";
}
Code segment - editform2.php
if (isset($_POST["delete"])) {
$id = $_GET["id"];
include("dbconnect.inc.php");
$query3 = "DELETE FROM Sport WHERE SportId=$id";
mysqli_query($con, $query3);
echo "<h1>Deleted Successfully</h1>";
mysqli_close($con);
}
UPDATE!!!! I get this error now
DogSport is in use.Error: DELETE FROM Sport WHERE SportId=2
Cannot delete or update a parent row: a foreign key constraint fails (dogsport
.orders
, CONSTRAINT orders_ibfk_1
FOREIGN KEY (EqId
) REFERENCES equipment
(EqId
))