Untitled
unknown
plain_text
a year ago
6.3 kB
5
Indexable
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Product</title>
<style type="text/css">
body {
background: linear-gradient(to top, rgb(72, 65, 65), #faebd7);
background-repeat: no-repeat;
background-size: 100%;
background-attachment: fixed;
}
h1 {
margin-top: 7%;
color: #000000;
text-align: center;
}
div {
padding-top: 2%;
background-color: rgb(72, 65, 65);
border: 3px rgb(176, 163, 63) outset;
border-radius: 6px;
text-align: center;
margin: 35%;
margin-top: 10%;
width: 30%;
height: auto;
}
label {
color: white;
}
input[type="submit"] {
background-color: bisque;
border: 3px grey solid;
border-radius: 10px;
font-family: serif;
font-size: 16px;
}
input[type="file"] {
margin-left: 39%;
font-family: serif;
font-size: 16px;
}
input[type="submit"]:hover {
border: 3px grey double;
opacity: 70%;
}
</style>
</head>
<body>
<h1>Add Product</h1>
<div>
<form action="" method="post" enctype="multipart/form-data">
<!-- Product Information -->
<label for="name">Product Name:</label><br>
<input type="text" id="name" name="name" required placeholder="Write the product name"><br><br>
<label for="desc">Description:</label><br>
<input type="text" id="desc" name="desc" required placeholder="Write the product description"><br><br>
<label for="stock">Stock:</label><br>
<input type="text" id="stock" name="stock" required placeholder="Write the number of stock"><br><br>
<label for="price">Price:</label><br>
<input type="text" id="price" name="price" required placeholder="Write the product price"><br><br>
<label for="weight">Weight:</label><br>
<input type="text" id="weight" name="weight" required placeholder="Write the product weight"><br><br>
<label for="img">Insert Image</label><br>
<input type="file" name="img"><br><br>
<!-- Brand and Model Selection -->
<label for="brand">Select Car Brand:</label><br>
<select name="brand" id="brand" onchange="this.form.submit()">
<option value="">Select a brand</option>
<?php
$conn = mysqli_connect("localhost", "root", "", "wizmi");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$brand_stmt = "SELECT DISTINCT brand FROM car_brand";
$brand_result = mysqli_query($conn, $brand_stmt);
while ($row = mysqli_fetch_assoc($brand_result)) {
$selected = (isset($_POST['brand']) && $_POST['brand'] == $row['brand']) ? 'selected' : '';
echo "<option value='" . $row['brand'] . "' $selected>" . $row['brand'] . "</option>";
}
?>
</select><br><br>
<?php
if (isset($_POST['brand'])) {
$_SESSION['brand'] = $_POST['brand'];
$brand = $_POST['brand'];
echo "<label for='model'>Select Model:</label><br>";
echo "<select name='model' id='model'>";
echo "<option value=''>Select a model</option>";
$model_stmt = "SELECT model FROM car_brand WHERE brand = '$brand'";
$model_result = mysqli_query($conn, $model_stmt);
while ($row = mysqli_fetch_assoc($model_result)) {
echo "<option value='" . $row['model'] . "'>" . $row['model'] . "</option>";
}
echo "</select><br><br>";
}
?>
<input type="submit" name="add_product" value="Add Product">
</form>
</div>
<?php
if (isset($_POST['add_product'])) {
$name = $_POST["name"];
$desc = $_POST["desc"];
$stock = $_POST["stock"];
$price = $_POST["price"];
$weight = $_POST["weight"];
$img = $_FILES["img"]["name"];
$brand = $_SESSION['brand'] ?? '';
$model = $_POST['model'] ?? '';
if (empty($brand) || empty($model)) {
echo "<p style='color: red; text-align: center;'>Please select both brand and model.</p>";
} else {
// Insert product
$stmt = "INSERT INTO product (Name, Description, Stock, price, weight, img)
VALUES ('$name', '$desc', '$stock', '$price', '$weight', '$img')";
$result = mysqli_query($conn, $stmt);
if ($result) {
// Get the product_id of the newly added product
$product_id = mysqli_insert_id($conn);
// Get the brand_id for the selected brand and model
$brand_stmt = "SELECT id FROM car_brand WHERE brand = '$brand' AND model = '$model'";
$brand_result = mysqli_query($conn, $brand_stmt);
$brand_row = mysqli_fetch_assoc($brand_result);
$brand_id = $brand_row['id'];
// Insert into associative table
$assoc_stmt = "INSERT INTO prod_sutable_cars (product_id, car_brand_ID)
VALUES ('$product_id', '$brand_id')";
mysqli_query($conn, $assoc_stmt);
echo "<p style='color: green; text-align: center;'>$name was successfully added.</p>";
} else {
echo "<p style='color: red; text-align: center;'>Error: $name was not added.</p>";
}
}
mysqli_close($conn);
}
?>
</body>
</html>Editor is loading...
Leave a Comment