Reputation: 268
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
Upvotes: 1
Views: 5895
Reputation: 7228
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>
Upvotes: 1
Reputation: 68486
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>';?>
Upvotes: 1