简体   繁体   中英

Script for deleting table rows not working in MySQL & PHP

I want to delete a table row from my database with MySQL and PHP. I have searched through my code and I can't figure out what I'm doing wrong. I think I am close to getting it, probably something simple that I'm not realizing.

If I hover over the delete link there is a link showing with the correct ID number of the row to delete. But if I click it, it isn't working. It just refreshes the screen.

This is my code for 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 -->

This is my delete.php code:

<?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 is deprecated, instead of this use 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);
?>    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM