UPDATE FOTO FORMATO BINARIO Y NO BINARIO MYSQL PHP PDO JSON

 

FETCH post json api update data and url photos not binary mysql pdo php

 

Here's how you can update data with photos in MySQL using PDO:

 

Modify your database table to have a column for the image file path. Let's call this column "photo_path".

 

Modify your PHP script to handle the API request. This script will receive the JSON data and image file in the request body, extract the data, and save the image file to a directory on your server. Here's an example of what the script could look like:

 

php

Copy code

<?php

// Get the JSON data from the request body

$json_data = file_get_contents('php://input');

 

// Decode the JSON data into an array

$data = json_decode($json_data, true);

 

// Get the image file from the request

$image = $_FILES['photo'];

 

// Save the image file to a directory on your server

$target_dir = "uploads/";

$target_file = $target_dir . basename($image["name"]);

move_uploaded_file($image["tmp_name"], $target_file);

 

// Connect to the database using PDO

$db = new PDO('mysql:host=localhost;dbname=my_database', 'username', 'password');

 

// Prepare the SQL statement to update the data and image path

$stmt = $db->prepare('UPDATE my_table SET name=:name, age=:age, photo_path=:photo_path WHERE id=:id');

 

// Bind the data to the SQL parameters

$stmt->bindParam(':id', $data['id']);

$stmt->bindParam(':name', $data['name']);

$stmt->bindParam(':age', $data['age']);

$stmt->bindParam(':photo_path', $target_file);

 

// Execute the SQL statement

$stmt->execute();

?>

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="edit.php?id=' . htmlspecialchars($row['id']) . '">Edit</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. Also, the example code for editing the data in the database assumes that you have a separate "edit.php" file that handles the editing of the data.

Comentarios

Entradas populares de este blog

Copia de Seguridad de Archivo MS Access (.mdb) a Pendrive con VB6

INSERTAR Y UPDATE FECHA CON PHP MYSQL EN FORMULARIO METODO POST CON JQUERY ETIQUETA DATE

DETECTAR DIRECCION IP CON PHP Y JS