INSERTAR FOTO FORMATO BINARIO Y NO BINARIO MYSQL PHP PDO JSON

 FETCH post json api insert data and url photos mysql pdo php display table html

To insert data and photos using a POST JSON API in PHP and PDO to a MySQL database, you can follow the steps outlined below:

 

Create a database table to store the data and images. Let's call the table "my_table". The table should have columns for the data you want to store (such as name, age, etc.) and a column to store the image data (in binary format).

 

Create a 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 insert it into the database. 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_data = file_get_contents($_FILES['photo']['tmp_name']);

 

// Connect to the database using PDO

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

 

// Prepare the SQL statement to insert the data and image

$stmt = $db->prepare('INSERT INTO my_table (name, age, photo) VALUES (:name, :age, :photo)');

 

// Bind the data to the SQL parameters

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

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

$stmt->bindParam(':photo', $image_data, PDO::PARAM_LOB);

 

// 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 name, age, photo 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></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="data:image/jpeg;base64,' . base64_encode($row['photo']) . '"></td>';

    echo '</tr>';

}

echo '</table>';

?>

Note: This code assumes that the image data is stored in the database as a binary blob. If you're using a different method to store the images (such as storing them on disk and storing the file path in the database), you'll need to modify the code accordingly.

 

 

 

 

jose fernandez

not binary format

If you're not storing the image data in binary format in your database, you can still use PDO to insert and retrieve image data. Here's how you can modify the code:

 

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 insert the data and image path

$stmt = $db->prepare('INSERT INTO my_table (name, age, photo_path) VALUES (:name, :age, :photo_path)');

 

// Bind the data to the SQL parameters

$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 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></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 '</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.

 

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

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

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

CONVERTIR IMÁGEN JPG O PNG A WEBP CON PHP DE FORMA SIMPLE