dev version 1 #1

Open
kevin wants to merge 5 commits from dev into master
3 changed files with 71 additions and 1 deletions
Showing only changes of commit 441289aef4 - Show all commits

View File

@ -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>

View 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.";

View File

@ -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>