$stock) { if ($stock['stock'] === $handle) { $stocks[$i]['exchange_market'] = $stockexchg; $updated = true; break; } } if ($updated) { file_put_contents($jsonFile, json_encode($stocks, JSON_PRETTY_PRINT)); // Redirect back to stockvalues.php header('Location: stockvalues.php'); exit; } } header('Location: stockvalues.php'); exit; } // save.php - receives stock data via GET parameters and saves to stocks.json // Add this at the beginning of save.php if (isset($_GET['del']) && $_GET['del'] == 'yes' && isset($_GET['handle'])) { $handle = $_GET['handle']; $jsonFile = 'stocks.json'; if (file_exists($jsonFile)) { $stocks = json_decode(file_get_contents($jsonFile), true); $newStocks = array_filter($stocks, function($stock) use ($handle) { return $stock['stock'] !== $handle; }); $newStocks = array_values($newStocks); file_put_contents($jsonFile, json_encode($newStocks, JSON_PRETTY_PRINT)); } header('Location: stockvalues.php'); exit; } // Get parameters from URL $stock = isset($_GET['stock']) ? trim($_GET['stock']) : null; $price = isset($_GET['price']) ? trim($_GET['price']) : null; $currency = isset($_GET['currency']) ? trim($_GET['currency']) : null; $date = isset($_GET['date']) ? trim($_GET['date']) : date('Y-m-d'); $exchange_market = isset($_GET['exchange_market']) ? trim($_GET['exchange_market']) : 'N/A'; // Validate required fields if (!$stock || !$price) { die('Error: Missing required parameters. Use: save.php?stock=SYMBOL&price=PRICE¤cy=CURRENCY&date=DATE'); } // Prepare stock data $stockData = [ 'stock' => $stock, 'price' => floatval($price), 'currency' => $currency, 'date' => $date, 'timestamp' => date('Y-m-d H:i:s'), 'saved_at' => time(), 'exchange_market' => $exchange_market ]; // Load existing stocks.json $jsonFile = 'stocks.json'; $stocks = []; if (file_exists($jsonFile)) { $content = file_get_contents($jsonFile); $stocks = json_decode($content, true); if (!is_array($stocks)) $stocks = []; } // Add new stock (keep history, don't overwrite) $stocks[] = $stockData; // Save to file if (file_put_contents($jsonFile, json_encode($stocks, JSON_PRETTY_PRINT))) { // Display success page echo ''; echo ''; echo 'Stock Saved'; echo ''; echo ''; echo '
'; echo '

✅ Stock Saved Successfully!

'; echo '
'; echo 'Stock: ' . htmlspecialchars($stock) . '
'; echo 'Price: ' . htmlspecialchars($currency) . ' ' . htmlspecialchars($price) . '
'; echo 'Date: ' . htmlspecialchars($date) . '
'; echo 'Saved at: ' . date('Y-m-d H:i:s') . '
'; echo '
'; echo ''; echo ' '; echo '
'; } else { echo '
❌ Error: Could not write to stocks.json
'; } ?>