Script for deleting table rows not working in MySQL & PHP

huth2349

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');
?>
Hitesh Mundra

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql table not decreased in size after deleting rows

From Dev

Deleting rows from 3 tables in MySQL table

From Dev

Adding or deleting table rows with MySQL, jQuery and Ajax

From Dev

Deleting Row from users table PHP MYSQL

From Dev

Deleting a record in a PHP MySQL generated table

From Dev

PHP MySQL multiple rows not working

From Dev

Deleting rows in HTML table

From Dev

Efficiently deleting rows from one table where not matching another [MySQL]

From Dev

Deleting many rows from a big table MySql 5.5.46

From Dev

MySQL: deleting rows based on a condition with data from another table and NO JOIN

From Dev

Deleting Mysql rows with checkbox

From Dev

PHP, Mysql, Bootstrap Table not working

From Dev

Table style not working in PHP, MySql

From Dev

Deleting rows based on condition not working

From Dev

Deleting multiple rows from a table

From Dev

Dynamically added table rows not deleting

From Dev

Deleting multiple rows from a table

From Dev

Deleting specific row from a table in MySQL using PHP

From Dev

Deleting equal rows in mySQL 5.7.9?

From Dev

Conditionally deleting rows in MySQL Database

From Dev

update mysql table where rows are not in php array

From Dev

Display MySQL table ( number of rows are not same ) in PHP

From Dev

Get rows from mysql table to php array

From Dev

Deleting a row with Php & MySQL

From Dev

Deleting a row with Php & MySQL

From Dev

AJAX Script deleting table headers

From Dev

Serial number not working in PHP MySql Table

From Dev

PHP mysql query with temp table not working

From Dev

php mysql insert into table set is not working