109 lines
4.9 KiB
PHP
109 lines
4.9 KiB
PHP
<?php
|
||
require_once 'db/config.php';
|
||
|
||
$product_id = $_GET['id'] ?? null;
|
||
|
||
// ID yoksa veya geçersizse ana sayfaya yönlendir
|
||
if (!$product_id) {
|
||
$_SESSION['error'] = "Geçersiz ürün ID'si.";
|
||
header('Location: products.php');
|
||
exit();
|
||
}
|
||
|
||
$pdo = db();
|
||
|
||
// Form gönderildiğinde (POST request)
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$name = $_POST['name'] ?? '';
|
||
$type = $_POST['type'] ?? '';
|
||
$stock_quantity = $_POST['stock_quantity'] ?? 0;
|
||
$purchase_price = $_POST['purchase_price'] ?? 0.0;
|
||
$sale_price = $_POST['sale_price'] ?? 0.0;
|
||
$low_stock_threshold = $_POST['low_stock_threshold'] ?? 5;
|
||
|
||
if (!empty($name) && !empty($type) && is_numeric($stock_quantity) && is_numeric($purchase_price) && is_numeric($sale_price)) {
|
||
try {
|
||
$stmt = $pdo->prepare("UPDATE products SET name = ?, type = ?, stock_quantity = ?, purchase_price = ?, sale_price = ?, low_stock_threshold = ? WHERE id = ?");
|
||
$stmt->execute([$name, $type, $stock_quantity, $purchase_price, $sale_price, $low_stock_threshold, $product_id]);
|
||
$_SESSION['notification'] = "Ürün başarıyla güncellendi.";
|
||
header('Location: products.php');
|
||
exit();
|
||
} catch (PDOException $e) {
|
||
$_SESSION['error'] = "Veritabanı hatası: " . $e->getMessage();
|
||
}
|
||
} else {
|
||
$_SESSION['error'] = "Lütfen tüm alanları doğru bir şekilde doldurun.";
|
||
}
|
||
// Hata durumunda aynı sayfada kal, form tekrar dolsun
|
||
header("Location: edit-product.php?id=" . $product_id);
|
||
exit();
|
||
}
|
||
|
||
// Sayfa ilk yüklendiğinde (GET request)
|
||
try {
|
||
$stmt = $pdo->prepare("SELECT * FROM products WHERE id = ?");
|
||
$stmt->execute([$product_id]);
|
||
$product = $stmt->fetch(PDO::FETCH_ASSOC);
|
||
|
||
if (!$product) {
|
||
$_SESSION['error'] = "Ürün bulunamadı.";
|
||
header('Location: products.php');
|
||
exit();
|
||
}
|
||
} catch (PDOException $e) {
|
||
$_SESSION['error'] = "Veritabanı hatası: " . $e->getMessage();
|
||
header('Location: products.php');
|
||
exit();
|
||
}
|
||
|
||
require_once 'partials/header.php';
|
||
?>
|
||
|
||
<h1 class="mb-4">Ürünü Düzenle</h1>
|
||
|
||
<div class="card">
|
||
<div class="card-header">
|
||
<?php echo htmlspecialchars($product['name']); ?>
|
||
</div>
|
||
<div class="card-body">
|
||
<form action="edit-product.php?id=<?php echo $product_id; ?>" method="POST">
|
||
<div class="row">
|
||
<div class="col-md-6 mb-3">
|
||
<label for="name" class="form-label">Ürün Adı</label>
|
||
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($product['name']); ?>" required>
|
||
</div>
|
||
<div class="col-md-6 mb-3">
|
||
<label for="type" class="form-label">Ürün Tipi</label>
|
||
<select class="form-select" id="type" name="type" required>
|
||
<option value="jant" <?php echo ($product['type'] === 'jant') ? 'selected' : ''; ?>>Jant</option>
|
||
<option value="lastik" <?php echo ($product['type'] === 'lastik') ? 'selected' : ''; ?>>Lastik</option>
|
||
<option value="akü" <?php echo ($product['type'] === 'akü') ? 'selected' : ''; ?>>Akü</option>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
<div class="row">
|
||
<div class="col-md-3 mb-3">
|
||
<label for="stock_quantity" class="form-label">Stok Miktarı</label>
|
||
<input type="number" class="form-control" id="stock_quantity" name="stock_quantity" value="<?php echo htmlspecialchars($product['stock_quantity']); ?>" required>
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label for="purchase_price" class="form-label">Alış Fiyatı</label>
|
||
<input type="number" step="0.01" class="form-control" id="purchase_price" name="purchase_price" value="<?php echo htmlspecialchars($product['purchase_price']); ?>" required>
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label for="sale_price" class="form-label">Satış Fiyatı</label>
|
||
<input type="number" step="0.01" class="form-control" id="sale_price" name="sale_price" value="<?php echo htmlspecialchars($product['sale_price']); ?>" required>
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label for="low_stock_threshold" class="form-label">Düşük Stok Uyarısı</label>
|
||
<input type="number" class="form-control" id="low_stock_threshold" name="low_stock_threshold" value="<?php echo htmlspecialchars($product['low_stock_threshold']); ?>" required>
|
||
</div>
|
||
</div>
|
||
<button type="submit" class="btn btn-primary">Değişiklikleri Kaydet</button>
|
||
<a href="products.php" class="btn btn-secondary">İptal</a>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<?php require_once 'partials/footer.php'; ?>
|