Write a PHP script to accept product number from the user. Update the price of the
product and display an appropriate message.
<?php
// Sample product list (associative array: product_number => price)
$products = [
101 => 50,
102 => 75,
103 => 100,
104 => 120
];
// Initialize message
$message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$productNumber = $_POST['product_number'];
$newPrice = $_POST['new_price'];
// Check if product exists
if (array_key_exists($productNumber, $products)) {
$products[$productNumber] = $newPrice; // Update price
$message = "Product #$productNumber price updated to $$newPrice successfully.";
} else {
$message = "Product #$productNumber not found!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Product Price</title>
</head>
<body>
<h2>Update Product Price</h2>
<form method="post">
Product Number: <input type="number" name="product_number" required><br><br>
New Price: <input type="number" name="new_price" step="0.01" required><br><br>
<input type="submit" value="Update Price">
</form>
<?php
if (!empty($message)) {
echo "<h3>$message</h3>";
}
?>
</body>
</html>
