0
I am uploading several images and storing them in the bank, so far it is perfect. The records are recorded, separated by comma in the field materialDocumento
on the table tb_material
.
The problem is that I am not able to delete a specific image that is selected when necessary.
Could you help me?
The files are recorded in the database as follows:
498818_03c4414ce0324292a51e63009f513a82_mv2.jpg, 64223640_2226772990741441_1192728863528976384_o.jpg, 64254162_1912126265586260_4480819099357675520_o.jpg, 25044125149117.jpg
php.
<a href="material-edita.php?edita_material_ID=<?php echo $_GET['edita_material_ID']; ?>&imagem_material=<?php echo $img; ?>"><button type="submit" class="btn btn-large btn-danger" name="btn-deletar-imagem"><i class="fa fa-trash"></i> Excluir Imagem Material</button></a>
<?php
if (isset($_GET['edita_material_ID'])) {
$materialID = $_GET['edita_material_ID'];
extract($classeMaterial->getID($materialID));
$mDocumento = $_GET['imagem_material'];
if ($classeMaterial->deletarArqImg($materialID, $mDocumento)) {
var_dump($mDocumento);
}
}
?>
class.classe_material.php
<?php
public function deletarArqImg($materialID, $mDocumento)
{
try {
$stmt = $this->db->prepare("DELETE FROM tbl_material WHERE materialID = :materialID AND materialDocumento = :mDocumento");
$stmt->bindparam(":mDocumento", $mDocumento);
$stmt->bindparam(":materialID", $materialID);
$stmt->execute();
return true;
} catch (PDOException $e) {
echo $e->getMessage();
return false;
}
}
?>
If you just want to delete a specific image, it doesn’t make much sense to give
DELETE
in the entire record. It would not be better to make aUPDATE
removing the image from the list?– Woss
The simplest would be to record each image with an id, now being together you will have to retrieve the record, and separate with an explode, which will generate an array and then find the respective index... That is, rethink the way to save the images...
– MagicHat
Yes Anderson, I agree. But when I give UPDATE, it deletes all the records and adds the record I wanted to delete. I believe the explosion could solve as Magichat commented. Could give me an orientation of how to do it?
– Fabio
Fabio, the exploding solution will give you a lot more headache than remodeling this section in your database. If you’re messing with this, redo it, waste a little more time. Create an image-only table, each image with an id, and associate with the material id.
– MagicHat
Precisely, since you are using a relational bank, you have no reason to benefit from it to solve the problem.
– Woss