你的朋友是这里的编码初学者,我需要你的帮助来满足我的要求。实际上,我正在创建自己的库存管理系统,并且只是坚持创建一个条件......
你的朋友是这里的编码初学者,
我需要你的帮助来满足我的要求。
实际上,我正在创建自己的库存管理系统,并且只是在我的 php 页面中创建一个条件来发布我从供应商处收到的需求。
我已经制作了 html 表单来填写产品及其项目,这些项目基于我的结构化数据库“sql”,包括收到的数量,
当我按下产品选择时,产品会显示产品表内的数据库,并且工作正常,但在项目选择中,选择选项会显示所有项目,而我只想通过引用所选产品来条件化项目的选择,
知道数据库中的表已通过外键关联,其中项目与产品表中的产品相关。
他们告诉我,我应该使用 ajax 来创建其产品所显示的选择项,但是作为初学者,我确实没有得到这样做的认可。
现在我将给你我的代码来协助你
mysql 预测数据库
first table : CREATE TABLE `stock_products` (
`idstkproduct` int(11) NOT NULL,
`idstksec` int(11) DEFAULT NULL,
`product_name` varchar(45) DEFAULT NULL,
`description` varchar(205) DEFAULT NULL,
`created_at` datetime DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
Example odf the inserted data inside the first table :
INSERT INTO `stock_products` (`idstkproduct`, `idstksec`, `product_name`, `description`, `created_at`) VALUES
(1, 2, 'super_star', 'non', '2024-01-26 17:45:24'),
(2, 1, 'darling', NULL, '2024-01-26 17:46:53'),
(4, 1, 'ruch', NULL, '2024-01-26 17:49:00'),
(5, 1, 'anita', NULL, '2024-01-26 17:49:00'),
(6, 1, 'x_tasy', NULL, '2024-01-26 17:49:00'),
(7, 1, 'africa_queen', NULL, '2024-01-26 17:49:00'),
(8, 3, 'elegance', NULL, '2024-01-26 17:52:06'),
(9, 3, 'magic-k', NULL, '2024-01-26 17:56:20'),
(10, 3, 'leila', NULL, '2024-01-26 17:56:20');
-----------------------------------------------------------------------------
second table: CREATE TABLE `stock_designation` (
`idstkdesign` int(11) NOT NULL,
`idstkproduct` int(11) DEFAULT NULL,
`designation` varchar(45) DEFAULT NULL,
`description` varchar(205) DEFAULT NULL,
`created_at` datetime DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
Example for the inserted data inside the 2nd table :
INSERT INTO `stock_designation` (`idstkdesign`, `idstkproduct`, `designation`, `description`, `created_at`) VALUES
(1, 1, '1', 'noire couleur', '2024-01-26 18:18:03'),
(2, 1, 'white', 'white couleur', '2024-01-26 18:34:15'),
(3, 1, '950', 'couleur violet', '2024-01-26 18:34:15'),
(35, 2, '1', NULL, '2024-01-29 13:21:50'),
(36, 2, 'bleu', NULL, '2024-01-29 13:21:50'),
(37, 2, '613', NULL, '2024-01-29 13:21:50'),
(38, 2, '27', NULL, '2024-01-29 13:21:50'),
(39, 2, '350', NULL, '2024-01-29 13:21:50'),
(40, 2, '144', NULL, '2024-01-29 13:21:50'),
(51, 4, '1', NULL, '2024-01-29 13:39:31'),
(52, 4, '900', NULL, '2024-01-29 13:39:31'),
(53, 4, '33', NULL, '2024-01-29 13:39:31'),
(54, 4, '39', NULL, '2024-01-29 13:39:31'),
(55, 4, '27', NULL, '2024-01-29 13:39:31'),
(56, 4, '950', NULL, '2024-01-29 13:39:31'),
(75, 5, '1', NULL, '2024-01-29 13:46:59'),
(76, 5, '144', NULL, '2024-01-29 13:46:59'),
(77, 5, '33', NULL, '2024-01-29 13:46:59'),
(78, 5, 'night', NULL, '2024-01-29 13:46:59');
Now the relation code between the two tables :
and now the relation between these two tables : ALTER TABLE `stock_designation`
ADD PRIMARY KEY (`idstkdesign`),
ADD KEY `fk_stock_designation_stock_products` (`idstkproduct`);
供应商.php
<?php session_start(); ?>
<?php
if (!isset($_SESSION['user'])) header('location: login.php');
$_SESSION['table'] = 'stock_products';
$_SESSION['redirect_to'] = 'product-add.php';
$user = $_SESSION['user'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Product - Inventory Management System</title>
<?php include('partials/app-header-script.php');?>
</head>
<body>
<div id="dashboardMainContainer">
<?php include('partials/app-sidebar.php')?>
<?php include('./database/connection.php'); ?>
<div class="dashboard_content_container" id="dashboard_content_container">
<?php include('partials/app-topnav.php')?>
<div class="dashboard_content">
<div class="dashboard_content_main">
<div class="row">
<div class="column column-12">
<h1 class="sectionheader"><i class="fa fa-plus"></i> Create Arrived Marchandises</h1>
<div id="useraddformcontainer">
<form method="post" action="purchase_script.php" onsubmit="return validateForm()">
<h2>Purchase Transaction</h2>
<table>
<thead>
<tr>
<th>Product</th>
<th>Product Item</th>
<th>Quantity Recieved</th>
</tr>
</thead>
<tbody id="product_items">
<tr id="row1">
<td>
<select name="product_id[]" id="product_id_1" required onchange="fetchDesignations(1)">
<option value="">Select Product</option>
<?php
// Retrieve products from the database (already in your code)
$sql = "SELECT idstkproduct, product_name FROM stock_products ORDER BY product_name ASC";
$stmt = $conn->prepare($sql);
$stmt->execute();
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Populate the dropdown with product options
foreach ($products as $product) {
echo '<option value="' . $product['idstkproduct'] . '">' . $product['product_name'] . '</option>';
}
?>
</select>
</td>
<td>
<select name="designation_id[]" id="designation_id_1" required>
<option value="">Select Item</option>
<?php
// Retrieve department names from the database (Code added here)
$sql = "SELECT idstkdesign, designation FROM stock_designation ORDER BY designation ASC";
$stmt = $conn->prepare($sql);
$stmt->execute();
$designations = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Populate the dropdown with Product options
foreach ($designations as $designation) {
echo '<option value="' . $designation['idstkdesign'] . '">' . $designation['designation'] . '</option>';
}
?>
</select>
</td>
<td><input type="number" name="quantity[]" id="quantity_1" min="1" required></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2">
<button type="button" onclick="addRow()">Add Another Product Item</button>
</td>
</tr>
</tfoot>
</table>
<button type="submit">Record entry</button>
</form>
<?php if(isset($_SESSION['response'])) {
$response_message = $_SESSION['response']['message'];
$is_success = $_SESSION['response']['success'];
?>
<div class="responsemessage">
<p class="responsemessage <?= $is_success ? 'responsemessage__success' : 'responsemessage__error'?>">
<?= $response_message ?>
</p>
</div>
<?php unset($_SESSION['response']); }?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include('partials/app-scripts.php');?>
</body>
</html>
指定.js
function fetchDesignations(productId) {
// Use AJAX to send a request to fetch_designations.php
$.ajax({
url: "database/fetch_designations.php", // Ensure correct path
method: "POST",
data: { product_id: productId },
success: function(response) {
// Update the designation dropdown based on the response
$("#designation_id_1").empty(); // Clear existing options
$.each(response, function(index, designation) {
$("#designation_id_1").append($("<option></option>").val(designation.id).text(designation.name));
});
} // Closing curly brace for the success function
}); // Closing curly brace for the $.ajax call
} // Closing curly brace for the fetchDesignations function
fetch_designation.php
<?php
// Connect to the database (include your connection details)
// **Ensure the path to connection.php is accurate:**
require_once './database/connection.php'; // Adjust path if needed
// Check for database connection errors:
if (!$conn) {
error_log("Database connection failed: " . mysqli_connect_error());
die("Database connection error"); // Or handle the error gracefully
}
if (isset($_POST['product_id'])) {
$product_id = $_POST['product_id'];
// Prepare and execute the query to fetch designations for the product
$sql = "SELECT idstkdesign, designation FROM stock_designation WHERE idstkproduct = ?";
$stmt = $conn->prepare($sql);
$stmt->execute([$product_id]);
// Fetch results as associative array
$designations = $stmt->fetchAll(PDO::FETCH_ASSOC);
// **Debugging:** Check the fetched data before encoding:
print_r($designations); // Or use var_dump($designations);
// Encode the designations as JSON and send as response
header('Content-Type: application/json'); // Set content type for JSON
echo json_encode($designations);
}
?>
商品选择仍然显示所有商品(不受我选择的产品限制),我很沮丧,
我需要您的建议,关于我应该做什么、修改什么或其他什么才能实现我想要的目标