我有一个页面可以查看和删除数据库中的数据。删除是一个链接,它单击并将url中的变量传递给另一个页面以进行删除查询。例如,我的数据库总共有3行,我删除最后两行没有问题,但我无法删除第一行,因为删除是基于“描述”列删除记录,而不是“ID” “专栏。以下是提交页面和删除查询页面
viewreportdb.php
<!doctype html>
<head>
<meta charset="utf-8">
<title>Report Database </title>
<link href="style/fg_membersite.css" rel='stylesheet' type='text/css'>
</head>
<?php
include('config.php');
include('adminconfig.php');
$sql = "SELECT * FROM report ORDER BY ID";
$result = mysql_query($sql);
?>
<body>
<form method="POST" action="reportsave.php" onSubmit="return validate(this);">
<table id='fcolor2' class="viewdb3">
<tr>
<td style='border:2px solid black; background-color:#FF9933;' align=center> ID </td>
<td style='border:2px solid black; background-color:#FF9933;' align=center> Name </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Department </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Description </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Date </td>
</tr><?!-- missing close tr -->
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['ID'] ?></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['name'] ?></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['department'] ?></td>
<td><textarea rows="5" cols="50" name="description[]"><?php echo $row['description']; ?></textarea></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['date'] ?></td>
</tr><!-- missing close tr -->
<tr>
<td><input class='edit' type = 'text' name="ID[]" value='<?php echo $row['ID'] ?>' maxlength="50"/></td>
<td><input class='edit' type = 'text' name="name[]" value='<?php echo $row['name'] ?>' maxlength="50"></td>
<td><input class='edit' type = 'text' name="department[]" value='<?php echo $row['department'] ?>' maxlength="50"></td>
<td> </td>
<td><input class='edit' type = 'text' name="date[]" value='<?php echo $row['date'] ?>' maxlength="50"></td>
<?php echo "<td><a href='http://localhost/kinhock/deletereport.php?description=".$row['description']."'>Delete</a></td>";?>
</tr><!-- missing close tr -->
<?php
} ?>
<tr class='btn2'><td><input type='submit' name='submit' value='Submit' /></td></tr>
<tr class='btn2'><td><a href="http://localhost/kinhock/adminitem.php"> Back to Description </a></td></tr><!-- missing open & close tr-->
<tr class='btn2'><td> <a href="http://localhost/kinhock/adminlogin.php">Logout</a></td></tr>
</table>
</form>
<?php
$ID=$row['ID'];
$name=$row['name'];
$department=$row['department'];
$date=$row['date'];
$description=$row['description'];
?>
</body>
</html>
deletereport.php
<?php
include('adminconfig.php');
include('config.php');
$description = $_GET['description'];
$sql="DELETE FROM `report` WHERE `description`='$description'";
$result=mysql_query($sql);
if(!$result){
die('invalid query:'.mysql_error());
}else
?>
<p style="font-family:arial;color:#0066CC;font-size:30px;">One row deleted...</p>
<?php
header('Refresh:3; url=viewreportdb.php');
die;
?>
答案 0 :(得分:0)
请勿使用description
字段进行删除,因为urlencoding和更多因素可能会破坏此代码,即使重复描述也很有可能,不是吗?你的代码也是
最简单的SQL Injection方法!
在此处更改您的代码:
<?php echo "<td><a href='http://localhost/kinhock/
deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
您的SQL查询:
$id = intval($_GET['id']);
$sql="DELETE FROM `report` WHERE `id`='$id'";
答案 1 :(得分:0)
第1步
发送ID而非删除说明。
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
第2步
使用ID而不是说明进行删除。
$id = intval($_GET['id']);
$sql="DELETE FROM `report` WHERE `id`=$id";
答案 2 :(得分:0)
您应该在where子句和此行中使用ID而不是描述:
(变化)
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?description=".$row['description']."'>Delete</a></td>";?>
(到)
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
最后改变:
$description = $_GET['description'];
$sql="DELETE FROM `report` WHERE `description`='$description'";
为:
$id = $_GET['id'];
$sql="DELETE FROM `report` WHERE `ID`='$id'";
假设它是主ID或唯一ID,使用ID将更快更准确。
您可能还想使用:
$id = mysql_real_escape_string($_GET['id']);
防止SQL注入。
答案 3 :(得分:0)
我相信您的报告表的ID是基于用户ID的吗?为什么不这样做,每个报告都有一个ID(报告ID),User_ID(例如admin001)和描述。这样,如果admin001生成报告A,它将具有ID 1,如果admin001生成报告B,则它将具有ID 2. si,当您要删除报告A时使用
$sql = "delete from {tablename} where ID={report ID}";