forked from hemang-mishra/DBMSProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit_crop.php
More file actions
109 lines (88 loc) · 3.72 KB
/
submit_crop.php
File metadata and controls
109 lines (88 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
session_start();
include("db_connection.php");
// Check if the user is logged in
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
$f_id = $_SESSION['user_id']; // Get farmer's ID from session
// If the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Get the form data
$c_name = $_POST['c_name'];
$c_qty = intval($_POST['c_qty']);
$img_url = $_POST['img_url'];
$ppu = floatval($_POST['ppu']);
$unit = $_POST['unit'];
$shelf_life = intval($_POST['shelf_life']);
$c_desc = $_POST['c_desc'];
// Validate mandatory fields
if (empty($c_name) || empty($c_qty) || empty($ppu) || empty($unit)) {
die("Error: Please fill all required fields.");
}
// Get the max value of 'id' and increment by 1
$sql_max_id = "SELECT MAX(c_id) AS max_id FROM crop";
$result_max_id = $conn->query($sql_max_id);
if ($result_max_id && $result_max_id->num_rows > 0) {
$row_max_id = $result_max_id->fetch_assoc();
$new_id = $row_max_id['max_id'] + 1;
} else {
$new_id = 1; // Default to 1 if no rows exist
}
// Prepare SQL statement to insert the crop
$sql = "INSERT INTO crop (c_id, c_name, c_qty, img_url, ppu, unit, shelf_life, f_id, c_desc)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Error preparing statement: " . $conn->error);
}
$stmt->bind_param("isisdsiss", $new_id, $c_name, $c_qty, $img_url, $ppu, $unit, $shelf_life, $f_id, $c_desc);
// Execute the statement
if ($stmt->execute()) {
echo "Crop added successfully!";
header("Location: farmer_dashboard.php");
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Farmer Dashboard</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="css/add_crop.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<!-- Header -->
<?php include("header_f.php"); ?>
<div class="add-crop-section">
<h2>Add New Crop</h2>
<form action="add_crop.php" method="POST">
<label for="c_name">Crop Name:</label>
<input type="text" id="c_name" name="c_name" placeholder="Enter crop name" required>
<label for="c_qty">Quantity:</label>
<input type="number" id="c_qty" name="c_qty" placeholder="Enter crop quantity" required>
<label for="img_url">Image URL (optional):</label>
<input type="text" id="img_url" name="img_url" placeholder="http://example.com/image.jpg">
<label for="ppu">Price per Unit (₹):</label>
<input type="number" id="ppu" name="ppu" placeholder="Enter price per unit" required step="0.01">
<label for="unit">Unit of Measurement:</label>
<input type="text" id="unit" name="unit" placeholder="e.g., kg, bunch" required>
<label for="shelf_life">Shelf Life (days):</label>
<input type="number" id="shelf_life" name="shelf_life" placeholder="Enter shelf life in days">
<label for="c_desc">Description:</label>
<textarea id="c_desc" name="c_desc" placeholder="Provide a description of the crop" rows="4"></textarea>
<input type="hidden" name="f_id" value="<?php echo $_SESSION['user_id']; ?>"> <!-- Farmer ID -->
<button type="submit">Add Crop</button>
</form>
</div>
</body>
</html>