0% found this document useful (0 votes)
12 views4 pages

PHP Product Management System

Uploaded by

Rohit Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

PHP Product Management System

Uploaded by

Rohit Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

<?

php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Database credentials
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecommerceDB_2022wa86009";
// Connect to MySQL server
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database if not exists
$conn->query("CREATE DATABASE IF NOT EXISTS $dbname");
$conn->select_db($dbname);
// Create categories table if not exists (needed for foreign key)
$conn->query("CREATE TABLE IF NOT EXISTS categories (
category_id INT AUTO_INCREMENT PRIMARY KEY,
category_name VARCHAR(100) NOT NULL UNIQUE
)");
// Create products table if not exists
$table_sql = "CREATE TABLE IF NOT EXISTS products (
product_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
sku VARCHAR(30) NOT NULL UNIQUE,
category_id INT,
unit_price DECIMAL(10,2) NOT NULL,
quantity INT NOT NULL,
description TEXT,
FOREIGN KEY (category_id) REFERENCES categories(category_id) ON DELETE SET NULL
)";
$conn->query($table_sql);
// Function to sanitize input
function clean($data) {
return htmlspecialchars(trim($data));
}
// Handle Add / Update
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$product_name = clean($_POST['product_name']);
$sku = clean($_POST['sku']);
$category_id = intval($_POST['category_id']);
$unit_price = floatval($_POST['unit_price']);
$quantity = intval($_POST['quantity']);
$description = clean($_POST['description']);
$product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0;
if ($_POST['action'] == "Add") {
$stmt = $conn->prepare("INSERT INTO products (product_name, sku,
category_id, unit_price, quantity, description) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssidds", $product_name, $sku, $category_id, $unit_price,
$quantity, $description);
$stmt->execute();
$stmt->close();
} elseif ($_POST['action'] == "Update" && $product_id > 0) {
$stmt = $conn->prepare("UPDATE products SET product_name=?, sku=?,
category_id=?, unit_price=?, quantity=?, description=? WHERE product_id=?");
$stmt->bind_param("ssiddsi", $product_name, $sku, $category_id, $unit_price,
$quantity, $description, $product_id);
$stmt->execute();
$stmt->close();
}
header("Location: product_form.php");
exit;
}
// Handle Delete
if (isset($_GET['delete'])) {
$product_id = intval($_GET['delete']);
$stmt = $conn->prepare("DELETE FROM products WHERE product_id=?");
$stmt->bind_param("i", $product_id);
$stmt->execute();
$stmt->close();
header("Location: product_form.php");
exit;
}
// Handle Edit (prefill form)
$edit_product = null;
if (isset($_GET['edit'])) {
$product_id = intval($_GET['edit']);
$result = $conn->query("SELECT * FROM products WHERE product_id=$product_id");
$edit_product = $result->fetch_assoc();
}
// Fetch categories for dropdown
$categories = $conn->query("SELECT * FROM categories ORDER BY category_name ASC");
// Fetch all products with category names
$products = $conn->query("
SELECT p.product_id, p.product_name, [Link], c.category_name, p.unit_price,
[Link], [Link]
FROM products p
LEFT JOIN categories c ON p.category_id = c.category_id
ORDER BY p.product_id ASC
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Product Management</title>
<style>
body { font-family: Arial, sans-serif; margin: 30px; background-color: #f9f9f9; }
form { max-width: 600px; margin-bottom: 30px; background: #fff; padding: 20px;
border-radius: 8px; box-shadow: 0 0 5px #ccc; }
input[type="text"], input[type="number"], textarea, select {
width: 100%; padding: 8px; margin-top: 5px; margin-bottom: 10px; border: 1px
solid #ccc; border-radius: 4px;
}
input[type="submit"] {
padding: 8px 15px; background-color: #4CAF50; color: white; border: none;
border-radius: 4px; cursor: pointer;
}
input[type="submit"]:hover { background-color: #45a049; }
table {
width: 100%; border-collapse: collapse; background: #fff; box-shadow: 0 0 5px
#ccc; border-radius: 6px; overflow: hidden;
}
th, td {
border: 1px solid #ddd; padding: 8px; text-align: left;
}
th { background-color: #f2f2f2; }
footer {
margin-top: 40px; text-align: center; font-size: 14px; color: #666; padding:
15px 0;
border-top: 1px solid #ccc;
}
</style>
</head>
<body>
<h2>Product Management</h2>
<form action="product_form.php" method="POST">
<input type="hidden" name="product_id" value="<?php echo
$edit_product['product_id'] ?? ''; ?>">
<label>Product Name *</label>
<input type="text" name="product_name" maxlength="100" required value="<?php echo
$edit_product['product_name'] ?? ''; ?>">
<label>SKU *</label>
<input type="text" name="sku" maxlength="30" required value="<?php echo
$edit_product['sku'] ?? ''; ?>">
<label>Category</label>
<select name="category_id">
<option value="">-- Select Category --</option>
<?php
while ($cat = $categories->fetch_assoc()) {
$selected = ($edit_product && $edit_product['category_id'] ==
$cat['category_id']) ? 'selected' : '';
echo "<option value='{$cat['category_id']}'
$selected>{$cat['category_name']}</option>";
}
?>
</select>
<label>Unit Price *</label>
<input type="number" step="0.01" name="unit_price" required value="<?php echo
$edit_product['unit_price'] ?? ''; ?>">
<label>Quantity *</label>
<input type="number" name="quantity" required value="<?php echo
$edit_product['quantity'] ?? ''; ?>">
<label>Description</label>
<textarea name="description"><?php echo $edit_product['description'] ?? '';
?></textarea>
<input type="submit" name="action" value="<?php echo $edit_product ? 'Update' :
'Add'; ?>">
</form>
<h3>Existing Products</h3>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>SKU</th>
<th>Category</th>
<th>Price</th>
<th>Qty</th>
<th>Description</th>
<th>Actions</th>
</tr>
<?php
while ($row = $products->fetch_assoc()) {
echo "<tr>
<td>{$row['product_id']}</td>
<td>{$row['product_name']}</td>
<td>{$row['sku']}</td>
<td>{$row['category_name']}</td>
<td>{$row['unit_price']}</td>
<td>{$row['quantity']}</td>
<td>{$row['description']}</td>
<td>
<a href='product_form.php?edit={$row['product_id']}'>Edit</a> |
<a href='product_form.php?delete={$row['product_id']}' onclick=\"return
confirm('Delete this product?')\">Delete</a>
</td>
</tr>";
}
$conn->close();
?>
</table>
<!-- Footer -->
<footer>
&copy; <?php echo date("Y"); ?> | Developed by <strong>Sakshi</strong> (ID:
<strong>2022wa86009</strong>)
</footer>
</body>
</html>

You might also like