用于删除MySQL中不起作用的表行的脚本&;PHP


Script for deleting table rows not working in MySQL & PHP

我想用MySQL和PHP从数据库中删除一个表行。我已经搜索了我的代码,但我不知道我做错了什么。我想我已经接近了,可能是一些我没有意识到的简单的事情。

如果我将鼠标悬停在删除链接上,则会显示一个链接,其中包含要删除的行的正确ID号。但如果我点击它,它就不起作用了。它只是刷新屏幕。

这是我的index.php:代码

<!-- Table -->
<form action="index.php" method="get" id="dispatch">
<div class="col-md-8 column">

     <fieldset>
        <legend>Incident Board (Incidents in red are active)</legend>
         <div class="scroll-bar">
         <table>
             <thead>
             <tr>
                 <th>Incident #</th>
                 <th>Town</th>
                 <th>Location</th>
                 <th>Incident Type</th>
                 <th>Time/Date</th>
                 <th>Admin</th>
                 <th>Delete Entry</th>
            </tr>
             </thead>
             <tbody>
             <?php

  if( isset($_POST['town']) )
  {
    $town = $_POST['town'];
  }
  if( isset($_POST['location']) )
  {
  $location = $_POST['location'];
  }
  if( isset($_POST['incident_type']) )
  {
  $incident_type= $_POST['incident_type'];
  }
  if( isset($_POST['time_date']) )
  {
  $time_date= $_POST['time_date'];
  }
  if( isset($_POST['admin']) )
  {
  $admin = $_POST['admin'];
  }
  if( isset($_POST['id']) )
  {
  $id = $_POST['id'];
  }

    $db = mysqli_connect('localhost','root','') or die("Database error"); 
    mysqli_select_db($db, 'cad');  
    $result= mysqli_query($db, "SELECT * FROM `cad` ORDER BY `time_date` DESC LIMIT 20"); 

  while($row = mysqli_fetch_array($result))
    {
  $town     = $row['town'];
  $location    = $row['location'];
  $incident_type     = $row['incident_type'];
  $time_date = $row['time_date'];
  $admin    = $row['admin']; 
  $id    = $row['id']; 
echo "

                    <tr>
                        <td class='"id-center'">
                            ".$id."
                        </td>
                        <td >
                            ".$town."
                        </td>
                        <td>
                            ".$location."
                        </td>
                        <td >
                            ".$incident_type."
                        </td>
                        <td >
                            ".$time_date."
                        </td>
                        <td >
                            ".$admin."
                        </td>

                        <td>
                        <a href='"delete.php?id=$id'" name='"delete'" value='"$id'" class='"btn btn-primary btn-default center-1'"><span class='"glyphicon glyphicon-trash'"></span></a>
                        </td> 

                        </tr>";
    }
  mysqli_close($db);

  ?>
             </tbody>
             </table> 
                </div>
             </fieldset>
             </div>
             </form>
<!-- End -->

这是我的delete.php代码:

<?php
    $username = "root";
    $password = "";
    $hostname = "localhost";
    $dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
    $selected = mysql_select_db("cad", $dbhandle);
    mysql_query("DELETE FROM cad WHERE id = ".$_GET['id']."");
    header('location: index.php');
?>

mysql已被弃用,而不是使用mysqli

<?php
$username = "root";
$password = "";
$hostname = "localhost";
$con=mysqli_connect($hostname, $username, $password,"cad");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
// Perform queries 
mysqli_query($con,"DELETE FROM cad WHERE id = ".$_GET['id']."");
mysqli_close($con);
?>