Php从mysql数据库中删除项目

时间:2012-09-09 14:07:10

标签: php mysql html phpmyadmin

希望有人可以帮助我,我想我的功能是正确的。 我试图从我的数据库中检索一些数据并在每个被检索的项目上附加删除链接,这样当我点击删除时,它将删除具有删除功能的特定项目。

从数据库中检索项目的我的代码如下。

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$count = 1;
$y = mysql_query("SELECT * FROM transaction");
if(mysql_num_rows($y) != 0){

echo "<table bgcolor=\"white\" width=\"1000\" bordercolor=\"grey\" border=\"5\" >";
echo "<tr>
<td align=\"center\">No</td>
<td align=\"center\">Date</td>
<td align=\"center\">Current Balance</td>
<td align=\"center\">Avaliable Balance</td>
<td align=\"center\">Account Status</td>
<td align=\"center\">Delete Account</td>

</tr>";

while ($z = mysql_fetch_array($y, MYSQL_BOTH)){
        echo "<tr>
        <td align=\"center\">".$count++."</td>
        <td align=\"center\">".$z[1]."</td>
        <td align=\"center\">".$z[2]."</td>
        <td align=\"center\">".$z[3]."</td>
        <td align=\"left\" width=\"300\">".$z[4]."</td>
        <td><a href=\"delete.php\">delete</a></td>
        </tr>";
    }
    echo "</table>";
    }
?>

我要删除的代码

<?php
session_start();
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$id = $_GET['id'];
$sql = mysql_query("DELETE FROM transaction WHERE id='$id' LIMIT 1") or die   (mysql_error());
header("Location: vacct.php");
?>

我知道我错过了这里的逻辑,并希望有人可以指导我或向我展示简单的方法。目前我可以从数据库中成功检索我的项目,我唯一的问题是每次点击删除按钮时都能应用删除功能。

4 个答案:

答案 0 :(得分:1)

点击删除链接时,您必须传递id

<a href=\"delete.php?id=$z[theIdKey]\">

答案 1 :(得分:0)

<td><a href=\"delete.php\">delete</a></td>

如何将id传递给delete.php脚本?

答案 2 :(得分:0)

变化:

<td><a href=\"delete.php\">delete</a></td>

为:

<td><a href=\"delete.php?id=".$z[0]."\">delete</a></td>

如果$ z [0]是ID。

在你的delete.php中,请确保你也使用反引号转义单词“transaction”:

DELETE FROM `transaction` WHERE id=123

这是因为“transaction”是一个保留的mysql关键字。

请同时阅读SQL Injections

答案 3 :(得分:0)

使用以下代码。我已添加验证和加密

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$count = 1;
$y = mysql_query("SELECT * FROM transaction");
if(mysql_num_rows($y) != 0){

echo "<table bgcolor=\"white\" width=\"1000\" bordercolor=\"grey\" border=\"5\" >";
echo "<tr>
<td align=\"center\">No</td>
<td align=\"center\">Date</td>
<td align=\"center\">Current Balance</td>
<td align=\"center\">Avaliable Balance</td>
<td align=\"center\">Account Status</td>
<td align=\"center\">Delete Account</td>

</tr>";

while ($z = mysql_fetch_array($y, MYSQL_BOTH)){
        echo "<tr>
        <td align=\"center\">".$count++."</td>
        <td align=\"center\">".$z[1]."</td>
        <td align=\"center\">".$z[2]."</td>
        <td align=\"center\">".$z[3]."</td>
        <td align=\"left\" width=\"300\">".$z[4]."</td>
        <td><a href=\"delete.php?id=".base64_encode($z[0])."\">delete</a></td>
        </tr>";
    }
    echo "</table>";
    }
?>

code to delete

<?php
session_start();
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$id = base64_decode($_GET['id']);
if(!empty($id)){
$sql = mysql_query("DELETE FROM transaction WHERE id='$id' LIMIT 1") or die   (mysql_error());
}
header("Location: vacct.php");
?>