我的删除程序不能在php中工作

时间:2015-01-05 02:39:28

标签: php mysql

我的删除程序在php中不起作用。

以下代码显示信息,但删除代码不起作用,我在哪里犯错?

已建立连接,但删除代码无效,为什么?

connection.php

<?php

// Database Connection
$con = mysql_connect("localhost","sathishcst","geni7joy");
mysql_select_db("practice",$con);
?>

display.php

<?php
require_once 'connection.php';
$query = "select * from pays";
$data = mysql_query($query);
?>
<html>

<body>
<table border=”1″ cellpadding=”5″>
<tr>
<th>Id</th> <th>NewsList</th> <th>Anchor link</th> <th colspan=”2″>Action</th>
</tr>
<?php while($rec = mysql_fetch_array($data)) { ?>
<tr>
<td> <?php echo $rec['id']; ?> </td>
<td> <?php echo $rec['name']; ?> </td>
<td> <?php echo $rec['email']; ?> </td>
<td> <a href=”edit.php?id=<?php echo $rec['id']; ?>”>edit</a> </td>
<td> <a onClick="return confirm('Sure to delete!')" href="delete.php?id=<?php echo $rec['id']; ?>">delete</a> </td>
</tr>
<?php } ?>
</table>
</body>
</html>

delete.php

<?php
require_once “connect.php”;
$msg = “”;
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : “0″;
$query = “delete from tbemp where id=”.$id;
if(mysql_query($query)) {
header(“location:display.php”);
} else {
echo “unable to delete!”;
}
?>

2 个答案:

答案 0 :(得分:0)

您在delete.php文件中出错了。在where子句之后,您需要使用$_REQUEST['id']而不是$id

试试这段代码:

<?php
require_once "connect.php";
$msg = "";
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "0";
$query = "delete from tbemp where id=".$_REQUEST['id'];
if(mysql_query($query)) {
header("location:display.php");
} else {
echo "unable to delete!";
}
?>

答案 1 :(得分:0)

改变这个:

<?php
require_once “connect.php”;
$msg = “”;
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : “0″;
$query = “delete from tbemp where id=”.$id;
if(mysql_query($query)) {
header(“location:display.php”);
} else {
echo “unable to delete!”;
}
?>

用这个:

<?php
require_once "connection.php";
$msg = "";
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "0";
$query = "delete from tbemp where id=".$id;
if(mysql_query($query)) {
header("location:display.php");
} else {
echo "unable to delete!";
}
?>