简体   繁体   中英

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. 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:

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

<?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. This avoids the confusion of quotes. The url is then easy to use in link.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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