2011-01-27 45 views
1

我有简单的PHP函数生成html链接应删除记录,但删除链接只管理刷新页面。使用PHP删除记录时遇到问题。代码包括

我知道解决方案很简单,但我是PHP新手,所以请有人指点一下我的正确方向?谢谢。非常感谢。

fuctions.php

<?php 
    include('includes/connect.php'); 
    function getPosts() { 
     $query = mysql_query("SELECT * FROM posts") or die(mysql_error()); 
     while($post = mysql_fetch_assoc($query)) { 
      echo "<tr><td>" . $post['Title'] . "</td><td>" . $post['Author'] . "</td><td><a href=\"delete.php?id=" . $post['ID'] . "\">Delete</a><br /><a href=\"edit.php>?id=" . $post['ID'] . "\">Edit</a></td></tr>"; 
     } 
    } 
    function deletePost($id) { 
     $id = (int) $id; 
     mysql_query("DELETE FROM posts WHERE ID = '$id'") or die(mysql_error()); 
     header("Location: posts.php"); 
    } 
?> 

delete.php

<?php 
    include('includes/functions.php'); 
    deletePost($_GET['ID']); 
?> 
+0

您不会将名为`ID`的参数传递给您的脚本。 – 2011-01-27 14:40:43

+0

deletePost($ _ GET ['id']);区分大小写 – kjy112 2011-01-27 14:46:12

回答

4

在你delete.php文件,你称:

deletePost($_GET['ID']); 

然而,在你的链接使用:

delete.php?id= 

这是一个个案问题,使它们都是大写ID或小写id

0

检查你的套管。

您的GET paramets名字为id同时检查ID

0

打印您的SQL执行前删除命令的,我知道会回答你的问题。