1

我正在尝试将删除链接添加到页面上显示的每一行记录。到目前为止,我已经做到了这一点,但无法弄清楚如何使链接工作,以便在单击删除链接时,只会从数据库中删除该行数据。我对 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'>";
}
?>

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

4

2 回答 2

1

我发现在构建动态链接时最好先创建 url(您可以在测试期间回显它)。这避免了引号的混淆。然后该 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>
于 2013-11-01T22:21:11.060 回答
1

你的a href标签应该是 [ Missed quotes ]

<?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>';?> 
于 2013-11-01T15:30:02.077 回答