简体   繁体   English

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

[英]delete link not deleting any record in mysql database

I am trying to add a delete link to every row of record displayed on my page. 我试图将删除链接添加到页面上显示的每一行记录。 I have managed to that so far but cannot figure out how to make the link work so that when a delete link is clicked, only that row of data will be removed from the database. 到目前为止,我已经设法解决了这一问题,但无法弄清楚如何使链接正常工作,因此,当单击删除链接时,只会从数据库中删除该行数据。 I am farly new to php/mysql so pardon my questions. 我是php / mysql的新手,请原谅我的问题。 I have the following code so far.. 到目前为止,我有以下代码。

<?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>

.. and then in my delete.php file, I have the following code: ..然后在我的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'>";
}
?>

When the delete link is clicked, it just comes up with a blank screen. 单击删除链接后,它只会显示一个空白屏幕。 Any help is much appreciated! 任何帮助深表感谢! Thanks 谢谢

Your a href tag should be [ Missed quotes ] 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>';?> 

I find when constructing a dynamic link it is best to create the url (You can echo it during testing)first. 我发现在构建动态链接时,最好先创建url(您可以在测试过程中回显它)。 This avoids the confusion of quotes. 这避免了引号的混乱。 The url is then easy to use in link. 该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>

Example of link 链接示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM