删除链接不删除mysql数据库中的任何记录

时间:2013-11-01 15:29:05

标签: php html mysql sql pdo

我正在尝试向页面上显示的每一行记录添加删除链接。到目前为止,我已经设法做到了,但无法弄清楚如何使链接工作,以便在单击删除链接时,只从数据库中删除该行数据。我是php / mysql的新手,所以原谅我的问题。到目前为止我有以下代码..

<?php
require("common.php");

if(empty($_SESSION['user'])) 
{ 
header("Location: index.php"); 

die("Redirecting to index.php");
}

$result = $db->prepare("SELECT * FROM compliance_requirement");
$result->execute();

?>


<div class="compTable">
<table >
<tr>
<th>Compliance Name</th><th>Compliance Goal</th><th>Compliance Description</th>  <th>Options</th>
</tr>

<?php while($row = $result->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>

<td style='width: 200px;'><?php echo $row['ComplianceName']; ?></td>
<td style='width: 150px;'><?php echo $row['ComplianceGoal']; ?></td>
<td style='width: 400px;'><?php echo $row['ComplianceDescription']; ?></td>

<td style ='width: 250px;' ><?php echo '<a href="delete.php?action=delete&id=delete'.$row['ComplianceName'].'">Delete</a>';?>   

</td>
</tr>
<?php } 
?> 
</table>
</div>

..然后在我的delete.php文件中,我有以下代码:

<?php
require ('common.php');

if( isset($_GET['delete']) )
{
$id = $_GET['delete'];
$sql= $db->prepare("DELETE FROM compliance_requirement WHERE ComplianceName='$id'");
$sql->execute();
echo "<meta http-equiv='refresh' content='0;url=compliance.php'>";
}
?>

单击删除链接时,它只会出现一个空白屏幕。任何帮助深表感谢!感谢

2 个答案:

答案 0 :(得分:1)

您的 a href 标记应为[错过报价]

<?php echo '<a href="delete.php?action=delete&id='.$row['ComplianceName'].'">Delete</a>';?>  
<?php echo '<a href="delete.php?edit='.$row['ComplianceID'].'">Edit</a>';?> 
<?php echo '<a href="delete.php?invite='.$row['id'].'">Invite Obstacle</a>';?> 

答案 1 :(得分:0)

我发现在构建动态链接时,最好首先创建url(您可以在测试期间回显它)。这避免了引号的混淆。这个网址很容易在链接中使用。

<?php 
   while($row = $result->fetch(PDO::FETCH_ASSOC)){ 
     $url = "delete.php?action=delete&id=".$row['ComplianceName'];
     //echo $url;
?>
<tr>
  <td style='width: 200px;'><?php echo $row['ComplianceName']; ?></td>
  <td style='width: 150px;'><?php echo $row['ComplianceGoal']; ?></td>
  <td style='width: 400px;'><?php echo $row['ComplianceDescription']; ?></td>
  <td style ='width: 250px;' ><?php echo '<a href ='.$url.'>' ?> Delete </a>

链接示例

<td style ='width: 250px;' ><a href =delete.php?action=delete&id=name1> Delete </a>