2025-06-06 17:14:52 +08:00

132 lines
5.6 KiB
PHP

<link rel="stylesheet" href="../../index.css" type="text/css" />
<script src="../../home.js"></script>
<html>
<head>
<title>Add Fruit</title>
<style>
body {
align-items: center;
height: 100%;
width: 99%;
z-index: 1;
overflow-x: hidden;
overflow-y: auto;
background-image: none;
}
</style>
</head>
<body>
<h1>Add Fruit</h1>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Include the database connection file
include '../ConnectDB.php';
// Read the uploaded image file
$image = $_FILES["image"]["tmp_name"];
$imageData = addslashes(file_get_contents($image));
// Check if the uploaded file is an image
$mime = mime_content_type($image);
$allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($mime, $allowedMimeTypes)) {
die("Invalid file format. Only JPEG, PNG, and GIF images are allowed.");
}
// Check if the file size is within limits
$maxFileSize = 1024 * 1024; // 1 MB
if ($_FILES["image"]["size"] > $maxFileSize) {
die("File size exceeds the limit. Maximum allowed size is 1 MB.");
}
// Sanitize user input
$name = mysqli_real_escape_string($conn, $_POST["name"]);
$simpledesc = mysqli_real_escape_string($conn, $_POST["simpledesc"]);
$fulldesc = mysqli_real_escape_string($conn, $_POST["fulldesc"]);
$quantities = mysqli_real_escape_string($conn, $_POST["quantities"]);
$buyer_price = mysqli_real_escape_string($conn, $_POST["buyer_price"]);
$seller_price = mysqli_real_escape_string($conn, $_POST["seller_price"]);
$buyer_availability = mysqli_real_escape_string($conn, $_POST["buyer_availability"]);
$seller_availability = mysqli_real_escape_string($conn, $_POST["seller_availability"]);
// Insert data into the database
$sql_add_inventory = "INSERT INTO `inventory` (`name`, `img`, `simpledesc`, `fulldesc`, `quantities`, `buyer_price`, `seller_price`, `buyer_availability`, `seller_availability`) VALUES ('$name', '$imageData', '$simpledesc', '$fulldesc', '$quantities', '$buyer_price', '$seller_price', '$buyer_availability', '$seller_availability')";
$sql_add_buyer = "ALTER TABLE `buyer` ADD `$name` INT NOT NULL DEFAULT '0'";
$sql_add_seller = "ALTER TABLE `seller` ADD `$name` INT NOT NULL DEFAULT '0'";
// Execute the SQL statements
$res_1 = mysqli_query($conn, $sql_add_inventory);
$res_2 = mysqli_query($conn, $sql_add_buyer);
$res_3 = mysqli_query($conn, $sql_add_seller);
// Check if any query failed
if ($res_1 && $res_2 && $res_3) {
echo "Inventory added successfully!";
} else {
echo "Error adding inventory. Please try again.";
}
}
?>
<form method="POST" action="" enctype="multipart/form-data" onsubmit="return checkprice()">
<table style="text-align: right;">
<tr>
<td><label for="name">Name:</label></td>
<td colspan="3"><input type="text" id="name" name="name" required></td>
</tr>
<tr>
<td><label for="img">Img:</label></td>
<td width="0%"><input type="file" name="image" accept="image/*" required></td>
</tr>
<tr>
<td><label for="simpledesc">Simple Description:</label></td>
<td colspan="3"><textarea id="simpledesc" name="simpledesc" required></textarea></td>
</tr>
<tr>
<td><label for="fulldesc">Full Description:</label></td>
<td colspan="3"><textarea id="fulldesc" name="fulldesc" required></textarea></td>
</tr>
<tr>
<td><label for="quantities">Quantities (kg):</label></td>
<td colspan="3"><input type="number" id="quantities" name="quantities" required></td>
</tr>
<tr>
<td><label for="buyer_price">Buyer Price:</label></td>
<td colspan="3"><input type="number" id="buyer_price" name="buyer_price" required></td>
</tr>
<tr>
<td><label for="seller_price">Seller Price:</label></td>
<td colspan="3"><input type="number" id="seller_price" name="seller_price" required></td>
</tr>
<tr>
<td><label for="buyer_availability">Buyer Availability (available: 1, not available: 0):</label></td>
<td colspan="3"><input type="number" id="buyer_availability" name="buyer_availability" required></td>
</tr>
<tr>
<td><label for="seller_availability">Seller Availability (available: 1, not available: 0):</label></td>
<td colspan="3"><input type="number" id="seller_availability" name="seller_availability" required></td>
</tr>
<tr>
<td colspan="2"><button type="submit">Add Inventory</button></td>
</tr>
</table>
</form>
<script>
function checkprice() {
var buyPrice = parseInt(document.getElementById("buyer_price").value);
var sellPrice = parseInt(document.getElementById("seller_price").value);
if (buyPrice < sellPrice) {
alert("Buyer price cannot be lower than seller price!");
return false;
}
}
</script>
</body>
<html>