0

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');
?>
13
  • var_dump($_GET);exit; on delete.php and see what you are getting. Commented Jun 30, 2015 at 4:30
  • Is there a reason why you're using MySQLi in index.php, but then using mysql in delete.php? Commented Jun 30, 2015 at 4:33
  • i think your delete button is actually submitting the form, i.e. refreshing the form and the link is never followed. what happens if you manually go to delete.php?id=xx Commented Jun 30, 2015 at 4:44
  • delete.php is open to SQL injection. Just sayin'. Commented Jun 30, 2015 at 4:49
  • Also, better hope no bots crawl the page or everything will be deleted (once you get it working). Just sayin'. Commented Jun 30, 2015 at 4:52

1 Answer 1

1

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);
?>    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.