删除按钮在表行中不起作用

时间:2017-04-28 15:01:02

标签: php html mysql

我正在尝试在每一行上添加一个删除按钮,以便在按下按钮时可以删除记录。但删除功能不起作用,并显示“删除记录错误”。我不知道问题出在哪里。

请帮帮我。任何帮助将不胜感激。

这是我的代码:

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"); ?>

我的数据库: 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]

1 个答案:

答案 0 :(得分:0)

替换此

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

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

*尝试使用pdo