Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Database connection parameters
- $servername = 'localhost';
- $dbName = 'hardware_ni';
- $username = 'root';
- $password = '';
- // Establish a connection to the MySQL database
- $conn = new PDO("mysql:host=$servername;dbname=$dbName", $username, $password);
- // Check if the form is submitted
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {
- // Retrieve form data
- $category = isset($_POST['category']) ? $_POST['category'] : '';
- $productId = isset($_POST['productid']) ? $_POST['productid'] : '';
- $productName = isset($_POST['productname']) ? $_POST['productname'] : '';
- $productPrice = isset($_POST['productprice']) ? $_POST['productprice'] : '';
- $productDescription = isset($_POST['productdescription']) ? $_POST['productdescription'] : '';
- $productImage = isset($_FILES['productimage']['name']) ? $_FILES['productimage']['name'] : '';
- // Upload the product image to a desired directory (adjust the path accordingly)
- $targetDirectory = 'images/';
- $targetFile = $targetDirectory . basename($_FILES['productimage']['name']);
- if (move_uploaded_file($_FILES['productimage']['tmp_name'], $targetFile)) {
- echo 'Image uploaded successfully.';
- // Insert the product into the database
- $sql = "INSERT INTO products (category, p_id, p_name, p_price, p_desc, p_image)
- VALUES (:category, :productId, :productName, :productPrice, :productDescription, :productImage)";
- $stmt = $conn->prepare($sql);
- // Bind the values to the parameters
- $stmt->bindParam(':category', $category);
- $stmt->bindParam(':productId', $productId);
- $stmt->bindParam(':productName', $productName);
- $stmt->bindParam(':productPrice', $productPrice);
- $stmt->bindParam(':productDescription', $productDescription);
- $stmt->bindParam(':productImage', $productImage);
- // Execute the statement
- if ($stmt->execute()) {
- echo "Product added successfully!";
- // Redirect to a different page to avoid duplicate submissions
- header("Location: seller_product.php");
- exit;
- } else {
- echo "Error: " . $stmt->errorInfo()[2];
- }
- } else {
- echo 'Error uploading image.';
- }
- }elseif ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['delete'])) {
- // Check if the delete parameter is set in the URL
- $productId = $_GET['delete'];
- // Prepare the SQL statement to delete data from the "products" table
- $sql = "DELETE FROM products WHERE p_id = :productId";
- $stmt = $conn->prepare($sql);
- // Bind the product ID to the parameter
- $stmt->bindParam(':productId', $productId);
- // Execute the statement
- if ($stmt->execute()) {
- echo "Product deleted successfully!";
- } else {
- echo "Error: " . $stmt->errorInfo()[2];
- }
- }elseif ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['searchQuery'])) {
- // Get the search query from the form submission
- $searchResults = array(); // initialize the variable to an empty array
- // Perform the search query and populate $searchResults
- // Perform the search query and populate $searchResults
- $searchTerm = isset($_GET['search']) ? $_GET['search'] : '';
- $sql = "SELECT * FROM products WHERE p_name LIKE :searchTerm";
- $stmt = $conn->prepare($sql);
- $stmt->bindValue(':searchTerm', '%' . $searchTerm . '%');
- if ($stmt->execute()) {
- $searchResults = $stmt->fetchAll(PDO::FETCH_ASSOC);
- } else {
- echo "Error executing search query: " . $stmt->errorInfo()[2];
- }
- // Loop through the search results
- if (!empty($searchResults)) {
- foreach ($searchResults as $result) {
- // Access individual result properties
- $productId = $result['p_id'];
- $productName = $result['p_name'];
- $productPrice = $result['p_price'];
- // ...
- // Do something with the result
- echo "Product ID: $productId, Name: $productName, Price: $productPrice";
- }
- } else {
- echo "No results found.";
- }
- }
- // Prepare the SQL statement to retrieve data from the "products" table
- $sql = "SELECT * FROM products";
- $stmt = $conn->prepare($sql);
- // Execute the statement
- if ($stmt->execute()) {
- $lastInsertId = $conn->lastInsertId();
- if ($lastInsertId) {
- echo "Product added successfully with ID: " . $lastInsertId;
- } else {
- echo "Product added successfully, but retrieving the ID failed.";
- }
- } else {
- echo "Error: " . $stmt->errorInfo()[2];
- }
- // Fetch all rows from the result set as an associative array
- $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
- // Close the database connection
- $conn = null;
- ?>
- <!DOCTYPE html>
- <html lang="en" dir="ltr">
- <head>
- <meta charset="UTF-8">
- <title>Seller Dashboard</title>
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
- <!-- Boxicons CDN Link -->
- <link href='https://unpkg.com/boxicons@2.0.7/css/boxicons.min.css' rel='stylesheet'>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="css/bootstrap.min.css">
- <link rel="stylesheet" href="seller_product.css">
- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
- <!---Sidebar-->
- <div class="sidebar">
- <div class="d-flex flex-column align-items-center text-center mt-5 ">
- <img src="https://bootdey.com/img/Content/avatar/avatar7.png" alt="Admin" class="rounded-circle bg-white " width="150">
- <span class="logo_name text-white mt-3">Boss Amo</span>
- </div>
- <ul class="nav-links">
- <li>
- <a href="seller_product.html" class="active">
- <i class='bx bx-grid-alt'></i>
- <span class="links_name">Product</span>
- </a>
- </li>
- <li>
- <a href="dashboard_order_product.html">
- <i class='bx bx-grid-alt'></i>
- <span class="links_name">Orders</span>
- </a>
- </li>
- <li>
- <a href="dashboard_pastreview_product.html">
- <i class='bx bx-list-ul'></i>
- <span class="links_name">Past Reviews</span>
- </a>
- </li>
- <li>
- <a href="dashboard_orderhistory_product.html">
- <i class='bx bx-coin-stack'></i>
- <span class="links_name">Order History</span>
- </a>
- </li>
- <li>
- <a href="dashboard_help_account.html">
- <i class='bx bx-coin-stack'></i>
- <span class="links_name">Settings</span>
- </a>
- </li>
- <li class="log_out">
- <a href="home.html">
- <i class='bx bx-log-out'></i>
- <span class="links_name">Home</span>
- </a>
- </li>
- </ul>
- </div>
- <section class="home-section">
- <header>
- <nav class="navbar navbar-expand-lg navbar-light bg-light">
- <div class="container-fluid">
- <div class="collapse navbar-collapse" id="navbarSupportedContent">
- <ul class="navbar-nav me-auto mb-2 mb-lg-0">
- <li class="nav-item">
- <a href="dashboard_order_product.html" class="nav-link active" style="padding-left: 50px;padding-right: 50px;">
- <i class="fa-sharp fa-solid fa-house fa-sm" style="color: #000000;">  </i>Product </a>
- </li>
- <li class="nav-item">
- <a href="dashboard_order_service.html" class="nav-link active" style="padding-left: 50px;padding-right: 50px;">
- <i class="fa-sharp fa-solid fa-house fa-sm" style="color: #000000;">  </i>Service </a>
- </li>
- </ul>
- </div>
- <form action="" method="POST">
- <input class="form-control me-2" type="search" name="searchQuery" placeholder="Search" aria-label="Search">
- <button class="btn btn-outline-success" type="submit">
- <i class="fa-sharp fa-solid fa-magnifying-glass fa-sm" style="color: #000000;"></i>
- </button>
- </form>
- </div>
- </nav>
- </header>
- <!-- Prdduct Nav -->
- <div class="home-content">
- <ul class="nav nav-tabs px-5">
- <li class="active">
- <a data-toggle="tab" href="#home">All</a>
- </li>
- </ul>
- <!-- All -->
- <div class="tab-content">
- <div id="home" class="tab-pane fade in active">
- <div class="sales-boxes py-5 border-top ">
- <div class="recent-sales box ">
- <div>
- <table class="table table-striped" style="float: right;">
- <thead>
- <tr>
- <td style="width: 100px;">
- <button class="btn btn-primary"><a href="add_products.php" style="color:white"> Add Products</a> </button>
- <div class="modal fade bd-example-modal-lg" id="editmodal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
- <div class="modal-dialog modal-lg">
- <div class="modal-content py-5 px-5">
- <h2 id="modalTitle">Update Product</h2>
- <form action="update_product.php" method="POST" enctype="multipart/form-data">
- <div class="mb-3">
- <label for="productimage" class="form-label">Product Image</label>
- <input type="file" class="form-control-file" id="productimage" name="productimage" accept="image/*">
- </div>
- <div class="mb-3">
- <label for="category" class="form-label">Category</label>
- <select class="form-control" id="category" name="category">
- <option value="Machines">Machines</option>
- <option value="Tools">Tools</option>
- <option value="Paints">Paints</option>
- <option value="Paints">Hardware</option>
- </select>
- </div>
- <div class="mb-3">
- <label for="productid" class="form-label">Product ID</label>
- <input type="text" class="form-control" id="productid" name="productid" placeholder="Product ID" required>
- </div>
- <div class="mb-3">
- <label for="productname" class="form-label">Product Name</label>
- <input type="text" class="form-control" id="productname" name="productname" placeholder="Product Name" required>
- </div>
- <div class="mb-3">
- <label for="productprice" class="form-label">Product Price</label>
- <input type="number" class="form-control" id="productprice" name="productprice" placeholder="Product Price" required>
- </div>
- <div class="mb-3">
- <label for="productdescription" class="form-label">Product Description</label>
- <textarea class="form-control" id="productdescription" name="productdescription" rows="4" placeholder="Description here..." required></textarea>
- </div>
- <button type="submit" class="btn btn-warning" name="submit" id="submitBtn">Update Product</button>
- </form>
- </div>
- </div>
- </div>
- </tr>
- </thead>
- </table>
- </div>
- <!-- All -->
- <table class="table table-striped">
- <thead style="text-align: center;">
- <tr style="text-align: center;">
- <th scope="col" class="px-5" style="text-align: center;">Product(s)</th>
- <th scope="col" class="px-5" style="text-align: center;">Category</th>
- <th scope="col" class="px-5" style="text-align: center;">Product ID</th>
- <th scope="col" class="px-5" style="text-align: center;">Product Name</th>
- <th scope="col" class="px-5" style="text-align: center;">Price</th>
- <th scope="col" class="px-5" style="text-align: center;">Description</th>
- <th scope="col" class="px-5" style="text-align: center;">Action</th>
- </tr>
- </thead>
- <tbody>
- <tr class="name" style="text-align: center;">
- <tbody> <?php foreach ($products as $product): ?> <tr>
- <td class="px-5" style="width: 100px;">
- <img src="images/<?php echo $product['p_image']; ?>" alt="product name">
- </td>
- <td class="px-5" style="width: 150px; text-align:center"> <?php echo $product['category']; ?> </td>
- <td class="px-5" style="width: 150px; text-align:center"> <?php echo $product['p_id']; ?> </td>
- <td class="px-5" style="width: 350px; text-align:center"> <?php echo $product['p_name']; ?> </td>
- <td class="px-5" style="width: 200px; text-align:center"> <?php echo "₱ " .$product['p_price']; ?> </td>
- <td class="px-5" style="width:600px; text-align: justify;"> <?php echo $product['p_desc']; ?> </td>
- <td class="px-5" style="text-align:center">
- <a href="#">
- <button class="btn editProductBtn" style="background-color: none; outline: none;" data-toggle="modal" data-target=".bd-example-modal-lg"
- data-productid="<?php echo $product['p_id']; ?>"
- data-category="<?php echo $product['category']; ?>"
- data-productname="<?php echo $product['p_name']; ?>"
- data-productprice="<?php echo $product['p_price']; ?>"
- data-productdescription="<?php echo $product['p_desc']; ?>"
- data-productimage="<?php echo $product['p_image']; ?>">
- <span >
- <svg xmlns="http://www.w3.org/2000/svg" width="23" height="23" top="10" fill="black" class="bi bi-pencil-square" viewBox="0 0 16 16">
- <path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z" />
- <path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z" />
- </svg>
- </span>
- </button>
- </a>
- <a href="?delete=<?php echo $product['p_id']; ?>">
- <span>
- <svg xmlns="http://www.w3.org/2000/svg" width="23" height="23" fill="black" class="bi bi-trash" viewBox="0 0 16 16">
- <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5Zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5Zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6Z" />
- <path d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1ZM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118ZM2.5 3h11V2h-11v1Z" />
- </svg>
- </span>
- </a>
- </td>
- </tr> <?php endforeach; ?>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </div>
- </div>
- </section>
- <script>
- let sidebar = document.querySelector(".sidebar");
- let sidebarBtn = document.querySelector(".sidebarBtn");
- sidebarBtn.onclick = function() {
- sidebar.classList.toggle("active");
- if (sidebar.classList.contains("active")) {
- sidebarBtn.classList.replace("bx-menu", "bx-menu-alt-right");
- } else sidebarBtn.classList.replace("bx-menu-alt-right", "bx-menu");
- }
- </script>
- <script>
- $(document).ready(function() {
- var editModal = $('#editmodal');
- var form = editModal.find('form');
- $('.editProductBtn').click(function() {
- var productId = $(this).data('productid');
- // Fetch the existing product details using the productId from the database
- $.ajax({
- url: 'get_product_details.php',
- method: 'POST',
- data: { product_id: productId },
- dataType: 'json',
- success: function(response) {
- if (response.success) {
- var product = response.product;
- // Set the existing values in the modal fields
- form.find('#productimage').val(product.productimage);
- form.find('#category').val(product.category);
- form.find('#productid').val(product.productid);
- form.find('#productname').val(product.productname);
- form.find('#productprice').val(product.productprice);
- form.find('#productdescription').val(product.productdescription);
- // Set the form action to the PHP script that handles the update operation
- form.attr('action', 'update_product.php');
- // Show the modal
- editModal.modal('show');
- } else {
- // Handle error case
- console.log(response.error);
- }
- },
- error: function(xhr, status, error) {
- // Handle error case
- console.log(error);
- }
- });
- });
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement