DELETE FOTO FORMATO BINARIO Y NO BINARIO MYSQL PHP PDO JSON
FETCH post json api delete data and url photos
not binary mysql pdo php
Here's how you can delete data with photos in MySQL
using PDO:
Create an API endpoint to handle the DELETE request.
The API endpoint will receive the ID of the record to be deleted in the request
URL. Here's an example of what the PHP script could look like:
php
Copy code
<?php
// Get the ID from the request URL
$id = $_GET['id'];
// Connect to the database using PDO
$db = new
PDO('mysql:host=localhost;dbname=my_database', 'username', 'password');
// Prepare the SQL statement to retrieve the photo
path
$stmt = $db->prepare('SELECT photo_path FROM
my_table WHERE id=:id');
$stmt->bindParam(':id', $id);
$stmt->execute();
// Get the photo path from the result set
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$photo_path = $row['photo_path'];
// Delete the record from the database
$stmt = $db->prepare('DELETE FROM my_table WHERE
id=:id');
$stmt->bindParam(':id', $id);
$stmt->execute();
// Delete the photo file from the server
if (file_exists($photo_path)) {
unlink($photo_path);
}
?>
In the HTML code for the page where you want to
display the data, create a PHP script to retrieve the data from the database
and display it in a table. Here's an example of what the script could look
like:
php
Copy code
<?php
// Connect to the database using PDO
$db = new
PDO('mysql:host=localhost;dbname=my_database', 'username', 'password');
// Prepare the SQL statement to retrieve the data
$stmt = $db->prepare('SELECT id, name, age,
photo_path FROM my_table');
// Execute the SQL statement
$stmt->execute();
// Loop through the result set and display the data in
a table
echo '<table>';
echo
'<tr><th>Name</th><th>Age</th><th>Photo</th><th>Action</th></tr>';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo
'<tr>';
echo
'<td>' . htmlspecialchars($row['name']) . '</td>';
echo
'<td>' . htmlspecialchars($row['age']) . '</td>';
echo
'<td><img src="' . htmlspecialchars($row['photo_path']) .
'"></td>';
echo
'<td><a href="delete.php?id=' . htmlspecialchars($row['id']) . '">Delete</a></td>';
echo '</tr>';
}
echo
'</table>';
?>
Note: This code
assumes that the image data is stored as a file on your server, and the file
path is stored in the database. If you're using a different method to store the
images, you'll need to modify the code accordingly
Comentarios
Publicar un comentario