0

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.

admin.php

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))

Praba
  • 59
  • 1
  • 1
  • 9
  • 2
    "Fail without error" - Of course it fails without error, you aren't checking for any in your delete query. – Epodax Mar 23 '16 at 09:47
  • 2
    Your code is vulnerable to SQL injection and cross-site scripting. Please read [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) and [How to prevent XSS with HTML/PHP?](http://stackoverflow.com/questions/1996122/how-to-prevent-xss-with-html-php) for information on how to fix it. – Matt Raines Mar 23 '16 at 09:49
  • Problem is very strange. "Sport" table has 5 records. If I try to delete the first or second record (for example, sport id 1 and 2) it shows that they are deleted (no error shown) but they have actually not been deleted from the database table. But strange thing is that if I delete the records (sport id 3, 4, 5) they are actually deleted from the table. Problem exists only with the first and second records when deleting only. They are actually updated also. – Praba Mar 23 '16 at 09:50
  • The error shows, the `orders` table has a relation to column `EqId` in the table `equipment`. And I have strong reason to suspect the `equipment` has a relation to the `Sport` table up in the chain. – frz3993 Mar 23 '16 at 10:46
  • Yes sir, you are correct. "Equipment" table has a foreign key to SportId in "Sport" table . But I have also used on delete cascade like this. Why won't work? FOREIGN KEY(SportId) REFERENCES Sport(SportId) ON DELETE CASCADE . . . . – Praba Mar 23 '16 at 10:50

0 Answers0