File "insert.php"

Full Path: /home/analogde/www/ONSEMI/Dev tableau/insert.php
File size: 1.19 KB
MIME-type: text/x-php
Charset: utf-8

<?php
// Paramètres de connexion

$host = "analogdepat.mysql.db";
$dbname = "analogdepat";
$username = "analogdepat";
$password = "Un92pac007";

//$conn = new mysqli($host, $username, $password, $dbname);

/*CREATE TABLE tableau_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    data_json TEXT NOT NULL
);
*/

// Connexion à MySQL
try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die(json_encode(["message" => "Erreur de connexion: " . $e->getMessage()]));
}

// Lire le JSON reçu
$data = file_get_contents("php://input"); // Récupère le JSON brut

if (!$data) {
    die(json_encode(["message" => "Données JSON invalides."]));
}

// Préparer la requête pour insérer dans un seul champ
$sql = "INSERT INTO json_data (data_json) VALUES (:data_json)";

$stmt = $pdo->prepare($sql);

try {
    $stmt->execute(["data_json" => $data]);
    echo json_encode(["message" => "Données insérées avec succès !"]);
} catch (PDOException $e) {
    echo json_encode(["message" => "Erreur d'insertion: " . $e->getMessage()]);
}
?>