Reputation: 325
I'm showing data from mysql database in Table with below code
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['image']; ?></td>
<a title="Delete" href="deletestudent.php?id=<?php echo $row['id']; ?>"><button class="btn btn-danger btn-mini"> Delete</button></a></td>
I am able to delete the row with below code
<?php
include('../connect.php');
$id=$_GET['id'];
$result = $db->prepare("DELETE FROM student WHERE id= :memid");
$result->bindParam(':memid', $id);
$result->execute();
header ("location: students.php");
?>
Thanks in advance.
Upvotes: 0
Views: 6763
Reputation: 81
You can delete the image from the file system using:
unlink('path/to/file.jpg');
Upvotes: 0
Reputation: 1494
Before deleting the image from database you should first delet the image then if its successful delete the image from db.
you can delete the image with php's unlink function. before that make sure the file exists:
<?php
include('../connect.php');
$id=$_GET['id'];
// you must first retrieve data from database to get the image path that
//you have been saved in db
$stmt = $dbh->prepare("SELECT * FROM mytable WHERE id= :memid LIMIT 1");
$stmt->bindParam(':memid', $id);
$stmt->execute();
$record = $stmt->fetch();
//get image path
$imageUrl = $_DIR_.'/images/uploads/profile/'.$record['Image_name'];
//check if image exists
if(file_exists($imageUrl)){
//delete the image
unlink($imageUrl);
//after deleting image you can delete the record
$result = $db->prepare("DELETE FROM student WHERE id= :memid");
$result->bindParam(':memid', $id);
$result->execute();
}
header ("location: students.php");
?>
Upvotes: 3
Reputation: 1132
You can use unlink()
function to delete the image from directory.
If you image is the same directory of script and filename is in variable $id
you can delete it by using unlink($id)
.
If your image is in another directory you can use
$fileid = 'path/'.$id
unlink($fileid)
Here path/
is the location of the directory in which image is stored.
Upvotes: 0