Question

I am trying to delete from my database.

Direct link:

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

delete.php

include("connect.php");

if( isset($_GET['del']))
{
   $id =$_POST['id'];
   $sql = "DELETE termekek WHERE id = $id" ;
   echo "<a href='admin.php'>Back</a>"; 
}

But when I click the delete button nothing happens.

Can somebody help me ?

Was it helpful?

Solution

In your Direct link, you are missing single inverted quote:

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

It should be like:

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

Also, in your delete.php, you are getting the value via POST, while you are passing id as GET, So make it:

// $con or similar is the variable in your connect.php
include("connect.php");

if( isset($_GET['del']))
{
  $id = mysqli_real_escape_string($con, $_GET['del']);    
  $sql = "DELETE FROM `termekek` WHERE id = $id" ;
  echo "<a href='admin.php'>Back</a>"; 
}

Now, you need to run the query also.

So this will work:

// $con or similar is the variable in your connect.php
include("connect.php");

if( isset($_GET['del']))
{
  $id = mysqli_real_escape_string($con, $_GET['id']);   
  $sql = "DELETE FROM `termekek` WHERE id = $id" ;
  mysqli_query($con, $sql);   
  echo "<a href='admin.php'>Back</a>"; 
}

Also, a small note (sounds obvious): Do not pass id or sensible data with GET method. Anybody can pass the id by merely changing the variable del from url to other id and that thing will be bad.

OTHER TIPS

The basic DELETE command is:

DELETE FROM {table_name} WHERE {condition_field} = {condition_value}

Try to change the your variable to the following content:

$sql = "DELETE FROM termekek WHERE id = $id" ;

If this don't solve your problem, try to catch the errors, using something like:

mysql_error(); (http://www.php.net/manual/pt_BR/function.mysql-error.php)

PDO:errorInfo(); (http://php.net/manual/pt_BR/pdo.errorinfo.php)

How do you execute your query?

Use :

$sql ="DELETE termekek FROM YOURTABLE WHERE id = $id"    
$pdoConnection->prepare($sql);
    $pdoConnection->execute();

And avoid using $_GET for important data...

Direct link:

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

And also change:delete.php

include("connect.php");

if( isset($_GET['del']))
{
   $id =$_GET['del'];
   $sql = "DELETE from termekek WHERE id = $id" ;
   echo "<a href='admin.php'>Back</a>"; 
}

Try this.

<?php 
include("connect.php");


    if( isset($_GET['del']) )
    {
$id = (int)$_GET['del'];
        $sql = "DELETE termekek WHERE id = $id" ;
       echo "<a href='admin.php'>Back</a>"; 

    }

?> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top