不从数据库PHP中删除

时间:2014-02-19 13:04:44

标签: php sql database sql-delete

我正在尝试从我的数据库中删除。

直接链接:

 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>"; 
}

但是当我点击删除按钮时没有任何反应。

有人能帮助我吗?

5 个答案:

答案 0 :(得分:0)

在您的直接链接中,您缺少单引号:

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

应该是这样的:

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

另外,在你的delete.php中,你是通过POST获取值,而你将id作为GET传递,所以就这样做:

// $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>"; 
}

现在,您还需要运行查询。

所以这会奏效:

// $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>"; 
}

另外,一个小注释(听起来很明显):不要使用GET方法传递id或敏感数据。任何人都可以通过仅将变量del从url更改为其他id来传递id,并且该事情将会很糟糕。

答案 1 :(得分:0)

基本的DELETE命令是:

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

尝试将您的变量更改为以下内容:

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

如果这不能解决您的问题,请尝试使用以下内容捕获错误:

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

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

答案 2 :(得分:-1)

您如何执行查询?

使用:

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

避免将$ _GET用于重要数据......

答案 3 :(得分:-2)

直接链接:

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

并且还要更改: 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>"; 
}

答案 4 :(得分:-3)

试试这个。

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

    }

?>