File size: 1.5Kb
<?php
// Include database configuration
require_once 'config/core.php';
if (isset($_POST['save'])) {
// Get the analytics script from the form
$analyticsScript = $_POST['analytics_script'];
try {
// Create PDO connection
$pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Save the script to a variable
$scriptVariable = $analyticsScript;
// Check if there is an existing row in the table
$sql = "SELECT COUNT(*) FROM google_analytics";
$result = $pdo->query($sql);
$rowCount = $result->fetchColumn();
if ($rowCount > 0) {
// If a row exists, update the script
$stmt = $pdo->prepare("UPDATE google_analytics SET analytics_script = :analytics_script WHERE id = 1");
$stmt->execute(['analytics_script' => $scriptVariable]);
} else {
// If no row exists, insert the script into the database
$stmt = $pdo->prepare("INSERT INTO google_analytics (analytics_script) VALUES (:analytics_script)");
$stmt->execute(['analytics_script' => $scriptVariable]);
}
// Redirect back to the google_analytics.php page
header("Location: google_analytics.php");
exit();
} catch (PDOException $e) {
// Handle database connection error
echo "Connection failed: " . $e->getMessage();
}
}
?>