0

I am trying to add a delete button on each row so that I can delete a record when the button is pressed. But the delete function doesn't work and shows "Error deleting record". I don't know where the problem is.

Please help me. Any help would be appreciated.

Here is my code:

managetimetable.php

<?php 
include("include/config.php");

$sql = "SELECT * FROM `time_table`";
$query = mysqli_query($link,$sql) or die(mysqli_error($link));    
?>

<table width="70%" cellpadding="5" cellspacing="5">
  <tr>
    <th><strong>ID</strong></th>
    <th><strong>Image</strong></th>
    <th></th>
  </tr>

  <?php while ($row = mysqli_fetch_array($query)) : ?>
    <tr>
      <td><?php echo $row['t_id']; ?></td>
      <td><?php echo $row['t_name']; ?></td>
      <td><a href='delete.php?id=".$row['t_id']."'>Delete</a></td>
    </tr>
  <?php endwhile; ?>
</table>

delete.php

<?php
$id = $_GET['id'];

include("include/config.php");

$sql = "DELETE FROM time_table WHERE t_id = $id"; 

if (mysqli_query($link, $sql)) {
    mysqli_close($link);
    header('Location: index.php');
    exit;
} else {
    echo "Error deleting record";
}
?>

config.php

<?php $link=mysqli_connect("localhost","root","","course_registration_system"); ?>

My database: Time_table

t_id  t_name                    t_image 
8     course_offered_2017.jpg   [BLOB - 246.1 KiB]
9     time_table_2017.jpg       [BLOB - 446.4 KiB]
Badacadabra
  • 8,043
  • 7
  • 28
  • 49
Pierrer
  • 13
  • 1
  • you need better error description `echo("Error description: " . mysqli_error($link));` something like this – mxr7350 Apr 28 '17 at 15:04
  • Your code is vulnerable to [**SQL injection attacks**](https://en.wikipedia.org/wiki/SQL_injection). You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 28 '17 at 15:05

1 Answers1

0

Replace this

   <td><a href='delete.php?id=".$row['t_id']."'>Delete</a></td>

with

   <td><a href='delete.php?id=<?php echo $row["t_id"];?>'>Delete</a></td>

*Try to use pdo

Omi
  • 3,954
  • 5
  • 21
  • 41