File "insert.php"

Full Path: C:/wamp64/www/PERSONAL/EntregaCarnet/backend/insert.php
File size: 2.25 KB
MIME-type: text/x-php
Charset: utf-8

<?php
header('Content-Type: application/json');
session_start();

// Conexión a la base de datos
$host = '192.200.100.40';
$dbname = 'almuerzo';
$user = 'SANMARINO';
$pass = 'sanmarino2021*';

try {
    $bdc = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $user, $pass);
    $bdc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $bdc->exec("SET NAMES utf8mb4");
} catch (PDOException $e) {
    echo json_encode(['status' => 'error', 'mensaje' => 'Error de conexión: ' . $e->getMessage()]);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nombre   = $_POST['nombre'] ?? '';
    $cedula   = $_POST['cedula'] ?? '';
    $cargo    = $_POST['cargo'] ?? '';
    $ejecucion  = $_POST['ejecucion'] ?? '';
    $cambio   = $_POST['cambio'] ?? '';
    $perdida  = $_POST['perdida'] ?? '';
    $firmaBase64 = $_POST['firma'] ?? '';

    // Validación básica
    if (empty($nombre) || empty($cedula) || empty($cargo)) {
        echo json_encode(['status' => 'warning', 'mensaje' => 'Por favor, completa los campos obligatorios.']);
        exit;
    }

    // Decodificar firma en base64
    if (preg_match('/^data:image\/(\w+);base64,/', $firmaBase64)) {
        $firmaBinaria = $firmaBase64;
    } else {
        $firmaBinaria = null;
    }

    try {
        $sql = "INSERT INTO planilla_registros (nombre, cedula, cargo, ejecucion, firma, fecha)
                VALUES (:nombre, :cedula, :cargo, :ejecucion, :firma, CURRENT_DATE())";

        $stmt = $bdc->prepare($sql);
        $stmt->bindParam(':nombre', $nombre);
        $stmt->bindParam(':cedula', $cedula);
        $stmt->bindParam(':cargo', $cargo);
        $stmt->bindParam(':ejecucion', $ejecucion);
        $stmt->bindParam(':firma', $firmaBinaria);

        if ($stmt->execute()) {
            echo json_encode(['status' => 'success', 'mensaje' => 'Datos guardados correctamente.']);
        } else {
            echo json_encode(['status' => 'error', 'mensaje' => 'No se pudo guardar en la base de datos.']);
        }
    } catch (PDOException $e) {
        echo json_encode(['status' => 'error', 'mensaje' => 'Error SQL: ' . $e->getMessage()]);
    }
} else {
    echo json_encode(['status' => 'error', 'mensaje' => 'Método no permitido']);
}