Advertisement
Kulas_Code20

prods

May 11th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.76 KB | None | 0 0
  1. <?php
  2. // Database connection parameters
  3. $servername = 'localhost';
  4. $dbName = 'hardware_ni';
  5. $username = 'root';
  6. $password = '';
  7.  
  8. // Establish a connection to the MySQL database
  9. $conn = new PDO("mysql:host=$servername;dbname=$dbName", $username, $password);
  10.  
  11. // Check if the form is submitted
  12. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {
  13. // Retrieve form data
  14. $category = isset($_POST['category']) ? $_POST['category'] : '';
  15. $productId = isset($_POST['productid']) ? $_POST['productid'] : '';
  16. $productName = isset($_POST['productname']) ? $_POST['productname'] : '';
  17. $productPrice = isset($_POST['productprice']) ? $_POST['productprice'] : '';
  18. $productDescription = isset($_POST['productdescription']) ? $_POST['productdescription'] : '';
  19. $productImage = isset($_FILES['productimage']['name']) ? $_FILES['productimage']['name'] : '';
  20.  
  21.  
  22. // Upload the product image to a desired directory (adjust the path accordingly)
  23. $targetDirectory = 'images/';
  24. $targetFile = $targetDirectory . basename($_FILES['productimage']['name']);
  25. if (move_uploaded_file($_FILES['productimage']['tmp_name'], $targetFile)) {
  26. echo 'Image uploaded successfully.';
  27.  
  28. // Insert the product into the database
  29. $sql = "INSERT INTO products (category, p_id, p_name, p_price, p_desc, p_image)
  30. VALUES (:category, :productId, :productName, :productPrice, :productDescription, :productImage)";
  31. $stmt = $conn->prepare($sql);
  32.  
  33. // Bind the values to the parameters
  34. $stmt->bindParam(':category', $category);
  35. $stmt->bindParam(':productId', $productId);
  36. $stmt->bindParam(':productName', $productName);
  37. $stmt->bindParam(':productPrice', $productPrice);
  38. $stmt->bindParam(':productDescription', $productDescription);
  39. $stmt->bindParam(':productImage', $productImage);
  40.  
  41. // Execute the statement
  42. if ($stmt->execute()) {
  43. echo "Product added successfully!";
  44. // Redirect to a different page to avoid duplicate submissions
  45. header("Location: seller_product.php");
  46. exit;
  47. } else {
  48. echo "Error: " . $stmt->errorInfo()[2];
  49. }
  50. } else {
  51. echo 'Error uploading image.';
  52. }
  53.  
  54.  
  55. }elseif ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['delete'])) {
  56. // Check if the delete parameter is set in the URL
  57. $productId = $_GET['delete'];
  58.  
  59. // Prepare the SQL statement to delete data from the "products" table
  60. $sql = "DELETE FROM products WHERE p_id = :productId";
  61. $stmt = $conn->prepare($sql);
  62.  
  63. // Bind the product ID to the parameter
  64. $stmt->bindParam(':productId', $productId);
  65.  
  66. // Execute the statement
  67. if ($stmt->execute()) {
  68. echo "Product deleted successfully!";
  69. } else {
  70. echo "Error: " . $stmt->errorInfo()[2];
  71. }
  72. }elseif ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['searchQuery'])) {
  73. // Get the search query from the form submission
  74. $searchResults = array(); // initialize the variable to an empty array
  75.  
  76. // Perform the search query and populate $searchResults
  77. // Perform the search query and populate $searchResults
  78. $searchTerm = isset($_GET['search']) ? $_GET['search'] : '';
  79. $sql = "SELECT * FROM products WHERE p_name LIKE :searchTerm";
  80. $stmt = $conn->prepare($sql);
  81. $stmt->bindValue(':searchTerm', '%' . $searchTerm . '%');
  82. if ($stmt->execute()) {
  83. $searchResults = $stmt->fetchAll(PDO::FETCH_ASSOC);
  84. } else {
  85. echo "Error executing search query: " . $stmt->errorInfo()[2];
  86. }
  87.  
  88. // Loop through the search results
  89. if (!empty($searchResults)) {
  90. foreach ($searchResults as $result) {
  91. // Access individual result properties
  92. $productId = $result['p_id'];
  93. $productName = $result['p_name'];
  94. $productPrice = $result['p_price'];
  95. // ...
  96.  
  97. // Do something with the result
  98. echo "Product ID: $productId, Name: $productName, Price: $productPrice";
  99. }
  100. } else {
  101. echo "No results found.";
  102. }
  103.  
  104.  
  105. }
  106. // Prepare the SQL statement to retrieve data from the "products" table
  107. $sql = "SELECT * FROM products";
  108. $stmt = $conn->prepare($sql);
  109.  
  110. // Execute the statement
  111. if ($stmt->execute()) {
  112. $lastInsertId = $conn->lastInsertId();
  113. if ($lastInsertId) {
  114. echo "Product added successfully with ID: " . $lastInsertId;
  115. } else {
  116. echo "Product added successfully, but retrieving the ID failed.";
  117. }
  118. } else {
  119. echo "Error: " . $stmt->errorInfo()[2];
  120. }
  121.  
  122. // Fetch all rows from the result set as an associative array
  123. $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
  124.  
  125. // Close the database connection
  126. $conn = null;
  127. ?>
  128.  
  129.  
  130. <!DOCTYPE html>
  131. <html lang="en" dir="ltr">
  132. <head>
  133. <meta charset="UTF-8">
  134. <title>Seller Dashboard</title>
  135. <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">
  136. <!-- Boxicons CDN Link -->
  137. <link href='https://unpkg.com/boxicons@2.0.7/css/boxicons.min.css' rel='stylesheet'>
  138. <meta name="viewport" content="width=device-width, initial-scale=1">
  139. <link rel="stylesheet" href="css/bootstrap.min.css">
  140. <link rel="stylesheet" href="seller_product.css">
  141. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  142. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  143. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
  144. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  145. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  146.  
  147. </head>
  148. <body>
  149. <!---Sidebar-->
  150. <div class="sidebar">
  151. <div class="d-flex flex-column align-items-center text-center mt-5 ">
  152. <img src="https://bootdey.com/img/Content/avatar/avatar7.png" alt="Admin" class="rounded-circle bg-white " width="150">
  153. <span class="logo_name text-white mt-3">Boss Amo</span>
  154. </div>
  155. <ul class="nav-links">
  156. <li>
  157. <a href="seller_product.html" class="active">
  158. <i class='bx bx-grid-alt'></i>
  159. <span class="links_name">Product</span>
  160. </a>
  161. </li>
  162. <li>
  163. <a href="dashboard_order_product.html">
  164. <i class='bx bx-grid-alt'></i>
  165. <span class="links_name">Orders</span>
  166. </a>
  167. </li>
  168. <li>
  169. <a href="dashboard_pastreview_product.html">
  170. <i class='bx bx-list-ul'></i>
  171. <span class="links_name">Past Reviews</span>
  172. </a>
  173. </li>
  174. <li>
  175. <a href="dashboard_orderhistory_product.html">
  176. <i class='bx bx-coin-stack'></i>
  177. <span class="links_name">Order History</span>
  178. </a>
  179. </li>
  180. <li>
  181. <a href="dashboard_help_account.html">
  182. <i class='bx bx-coin-stack'></i>
  183. <span class="links_name">Settings</span>
  184. </a>
  185. </li>
  186. <li class="log_out">
  187. <a href="home.html">
  188. <i class='bx bx-log-out'></i>
  189. <span class="links_name">Home</span>
  190. </a>
  191. </li>
  192. </ul>
  193. </div>
  194. <section class="home-section">
  195. <header>
  196. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  197. <div class="container-fluid">
  198. <div class="collapse navbar-collapse" id="navbarSupportedContent">
  199. <ul class="navbar-nav me-auto mb-2 mb-lg-0">
  200. <li class="nav-item">
  201. <a href="dashboard_order_product.html" class="nav-link active" style="padding-left: 50px;padding-right: 50px;">
  202. <i class="fa-sharp fa-solid fa-house fa-sm" style="color: #000000;">&nbsp&nbsp</i>Product </a>
  203. </li>
  204. <li class="nav-item">
  205. <a href="dashboard_order_service.html" class="nav-link active" style="padding-left: 50px;padding-right: 50px;">
  206. <i class="fa-sharp fa-solid fa-house fa-sm" style="color: #000000;">&nbsp&nbsp</i>Service </a>
  207. </li>
  208. </ul>
  209. </div>
  210. <form action="" method="POST">
  211. <input class="form-control me-2" type="search" name="searchQuery" placeholder="Search" aria-label="Search">
  212. <button class="btn btn-outline-success" type="submit">
  213. <i class="fa-sharp fa-solid fa-magnifying-glass fa-sm" style="color: #000000;"></i>
  214. </button>
  215. </form>
  216. </div>
  217. </nav>
  218. </header>
  219. <!-- Prdduct Nav -->
  220. <div class="home-content">
  221. <ul class="nav nav-tabs px-5">
  222. <li class="active">
  223. <a data-toggle="tab" href="#home">All</a>
  224. </li>
  225. </ul>
  226. <!-- All -->
  227. <div class="tab-content">
  228. <div id="home" class="tab-pane fade in active">
  229. <div class="sales-boxes py-5 border-top ">
  230. <div class="recent-sales box ">
  231. <div>
  232.  
  233. <table class="table table-striped" style="float: right;">
  234. <thead>
  235. <tr>
  236. <td style="width: 100px;">
  237. <button class="btn btn-primary"><a href="add_products.php" style="color:white"> Add Products</a> </button>
  238. <div class="modal fade bd-example-modal-lg" id="editmodal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  239. <div class="modal-dialog modal-lg">
  240. <div class="modal-content py-5 px-5">
  241. <h2 id="modalTitle">Update Product</h2>
  242. <form action="update_product.php" method="POST" enctype="multipart/form-data">
  243. <div class="mb-3">
  244. <label for="productimage" class="form-label">Product Image</label>
  245. <input type="file" class="form-control-file" id="productimage" name="productimage" accept="image/*">
  246. </div>
  247. <div class="mb-3">
  248. <label for="category" class="form-label">Category</label>
  249. <select class="form-control" id="category" name="category">
  250. <option value="Machines">Machines</option>
  251. <option value="Tools">Tools</option>
  252. <option value="Paints">Paints</option>
  253. <option value="Paints">Hardware</option>
  254. </select>
  255. </div>
  256. <div class="mb-3">
  257. <label for="productid" class="form-label">Product ID</label>
  258. <input type="text" class="form-control" id="productid" name="productid" placeholder="Product ID" required>
  259. </div>
  260. <div class="mb-3">
  261. <label for="productname" class="form-label">Product Name</label>
  262. <input type="text" class="form-control" id="productname" name="productname" placeholder="Product Name" required>
  263. </div>
  264. <div class="mb-3">
  265. <label for="productprice" class="form-label">Product Price</label>
  266. <input type="number" class="form-control" id="productprice" name="productprice" placeholder="Product Price" required>
  267. </div>
  268. <div class="mb-3">
  269. <label for="productdescription" class="form-label">Product Description</label>
  270. <textarea class="form-control" id="productdescription" name="productdescription" rows="4" placeholder="Description here..." required></textarea>
  271. </div>
  272. <button type="submit" class="btn btn-warning" name="submit" id="submitBtn">Update Product</button>
  273. </form>
  274. </div>
  275. </div>
  276. </div>
  277. </tr>
  278. </thead>
  279. </table>
  280. </div>
  281. <!-- All -->
  282. <table class="table table-striped">
  283. <thead style="text-align: center;">
  284. <tr style="text-align: center;">
  285. <th scope="col" class="px-5" style="text-align: center;">Product(s)</th>
  286. <th scope="col" class="px-5" style="text-align: center;">Category</th>
  287. <th scope="col" class="px-5" style="text-align: center;">Product ID</th>
  288. <th scope="col" class="px-5" style="text-align: center;">Product Name</th>
  289. <th scope="col" class="px-5" style="text-align: center;">Price</th>
  290. <th scope="col" class="px-5" style="text-align: center;">Description</th>
  291. <th scope="col" class="px-5" style="text-align: center;">Action</th>
  292. </tr>
  293. </thead>
  294. <tbody>
  295. <tr class="name" style="text-align: center;">
  296. <tbody> <?php foreach ($products as $product): ?> <tr>
  297. <td class="px-5" style="width: 100px;">
  298. <img src="images/<?php echo $product['p_image']; ?>" alt="product name">
  299. </td>
  300. <td class="px-5" style="width: 150px; text-align:center"> <?php echo $product['category']; ?> </td>
  301. <td class="px-5" style="width: 150px; text-align:center"> <?php echo $product['p_id']; ?> </td>
  302. <td class="px-5" style="width: 350px; text-align:center"> <?php echo $product['p_name']; ?> </td>
  303. <td class="px-5" style="width: 200px; text-align:center"> <?php echo "₱ " .$product['p_price']; ?> </td>
  304. <td class="px-5" style="width:600px; text-align: justify;"> <?php echo $product['p_desc']; ?> </td>
  305. <td class="px-5" style="text-align:center">
  306. <a href="#">
  307. <button class="btn editProductBtn" style="background-color: none; outline: none;" data-toggle="modal" data-target=".bd-example-modal-lg"
  308. data-productid="<?php echo $product['p_id']; ?>"
  309. data-category="<?php echo $product['category']; ?>"
  310. data-productname="<?php echo $product['p_name']; ?>"
  311. data-productprice="<?php echo $product['p_price']; ?>"
  312. data-productdescription="<?php echo $product['p_desc']; ?>"
  313. data-productimage="<?php echo $product['p_image']; ?>">
  314. <span >
  315. <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">
  316. <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" />
  317. <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" />
  318. </svg>
  319. </span>
  320. </button>
  321. </a>
  322. <a href="?delete=<?php echo $product['p_id']; ?>">
  323. <span>
  324. <svg xmlns="http://www.w3.org/2000/svg" width="23" height="23" fill="black" class="bi bi-trash" viewBox="0 0 16 16">
  325. <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" />
  326. <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" />
  327. </svg>
  328. </span>
  329. </a>
  330. </td>
  331. </tr> <?php endforeach; ?>
  332. </tbody>
  333. </table>
  334. </div>
  335. </div>
  336. </div>
  337. </div>
  338. </div>
  339. </section>
  340. <script>
  341. let sidebar = document.querySelector(".sidebar");
  342. let sidebarBtn = document.querySelector(".sidebarBtn");
  343. sidebarBtn.onclick = function() {
  344. sidebar.classList.toggle("active");
  345. if (sidebar.classList.contains("active")) {
  346. sidebarBtn.classList.replace("bx-menu", "bx-menu-alt-right");
  347. } else sidebarBtn.classList.replace("bx-menu-alt-right", "bx-menu");
  348. }
  349. </script>
  350. <script>
  351. $(document).ready(function() {
  352. var editModal = $('#editmodal');
  353. var form = editModal.find('form');
  354.  
  355. $('.editProductBtn').click(function() {
  356. var productId = $(this).data('productid');
  357.  
  358. // Fetch the existing product details using the productId from the database
  359. $.ajax({
  360. url: 'get_product_details.php',
  361. method: 'POST',
  362. data: { product_id: productId },
  363. dataType: 'json',
  364. success: function(response) {
  365. if (response.success) {
  366. var product = response.product;
  367.  
  368. // Set the existing values in the modal fields
  369. form.find('#productimage').val(product.productimage);
  370. form.find('#category').val(product.category);
  371. form.find('#productid').val(product.productid);
  372. form.find('#productname').val(product.productname);
  373. form.find('#productprice').val(product.productprice);
  374. form.find('#productdescription').val(product.productdescription);
  375.  
  376. // Set the form action to the PHP script that handles the update operation
  377. form.attr('action', 'update_product.php');
  378.  
  379. // Show the modal
  380. editModal.modal('show');
  381. } else {
  382. // Handle error case
  383. console.log(response.error);
  384. }
  385. },
  386. error: function(xhr, status, error) {
  387. // Handle error case
  388. console.log(error);
  389. }
  390. });
  391. });
  392. });
  393.  
  394. </script>
  395.  
  396.  
  397. </body>
  398. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement