add(button_cargar_json)
This commit is contained in:
parent
8c220428ca
commit
441289aef4
@ -26,6 +26,6 @@
|
||||
|
||||
<nav class="navbar navbar-dark bg-dark">
|
||||
<div class="container">
|
||||
<a href="/crud_php/index.php" class="navbar-brand">GESTOR DATASET</a>
|
||||
<a href="/gestor_dataset/index.php" class="navbar-brand">GESTOR DATASET</a>
|
||||
</div>
|
||||
</nav>
|
||||
28
server_side/import_json.php
Normal file
28
server_side/import_json.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
include("../db.php");
|
||||
|
||||
$table = isset($_GET['table']) ? $_GET['table'] : '';
|
||||
if (!preg_match('/^[a-zA-Z0-9_]+$/', $table)) {
|
||||
die("Nombre de tabla no permitido.");
|
||||
}
|
||||
|
||||
if (!isset($_POST['json'])) {
|
||||
die("No se recibió JSON.");
|
||||
}
|
||||
|
||||
$data = json_decode($_POST['json'], true);
|
||||
if (!is_array($data)) {
|
||||
die("JSON inválido.");
|
||||
}
|
||||
|
||||
$inserted = 0;
|
||||
foreach ($data as $row) {
|
||||
$instruction = isset($row['instruction']) ? mysqli_real_escape_string($conn, $row['instruction']) : '';
|
||||
$input = isset($row['input']) ? mysqli_real_escape_string($conn, $row['input']) : '';
|
||||
$output = isset($row['output']) ? mysqli_real_escape_string($conn, $row['output']) : '';
|
||||
$query = "INSERT INTO `$table` (instruction, input, output) VALUES ('$instruction', '$input', '$output')";
|
||||
if (mysqli_query($conn, $query)) {
|
||||
$inserted++;
|
||||
}
|
||||
}
|
||||
echo "Se importaron $inserted registros.";
|
||||
@ -55,6 +55,10 @@ $table = isset($_GET['table']) ? $_GET['table'] : '';
|
||||
<a href="add_dataset.php?table=<?php echo htmlspecialchars($table); ?>" class="btn btn-primary">
|
||||
<i class="bi bi-plus-circle"></i> Agregar Dataset
|
||||
</a>
|
||||
<label class="btn btn-success mb-0">
|
||||
<i class="bi bi-upload"></i> Cargar JSON
|
||||
<input type="file" id="jsonFile" accept=".json" style="display:none;">
|
||||
</label>
|
||||
<div id="stats" class="ms-3"></div>
|
||||
</div>
|
||||
<table id="example" class="display" style="width:100%">
|
||||
@ -116,6 +120,44 @@ $table = isset($_GET['table']) ? $_GET['table'] : '';
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
$('#jsonFile').on('change', function(e) {
|
||||
const file = e.target.files[0];
|
||||
if (!file) return;
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(evt) {
|
||||
try {
|
||||
const data = JSON.parse(evt.target.result);
|
||||
if (!Array.isArray(data)) {
|
||||
alert('El JSON debe ser un array de objetos.');
|
||||
return;
|
||||
}
|
||||
// Enviar cada objeto al backend
|
||||
$.ajax({
|
||||
url: 'import_json.php?table=<?php echo htmlspecialchars($table); ?>',
|
||||
method: 'POST',
|
||||
data: {
|
||||
json: JSON.stringify(data)
|
||||
},
|
||||
success: function(resp) {
|
||||
alert(resp);
|
||||
$('#example').DataTable().ajax.reload();
|
||||
},
|
||||
error: function() {
|
||||
alert('Error al importar el JSON.');
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
alert('JSON inválido.');
|
||||
}
|
||||
};
|
||||
reader.readAsText(file);
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user