I have an order creation form that allows me to enter information for several products to be linked to the order.
Each product can have production items, and I’d like to add the possibility of entering the number of products to be produced for each task.
The product and job parts of the form are dynamically generated by javascript.
The current problem is that my php file doesn’t handle the input corresponding to the quantity of products for the production tasks.
if i create an order with only 1 product, it correctly processes its production tasks and the quantity of each.
If I create an order with several products, the first one will be processed correctly, but for the subsequent products, the quantity of each task will not be retrieved and inserted.
Javascript product production posts form :
function addProductField() {
var productFields = document.getElementById("product_fields");
var index = productFields.childElementCount + 1;
fetch("production_posts.php").then(response => response.json()).then(data => {
var productionPostsOptions = data.map(post => `
<div class="form-check">
<input class="form-check-input" type="checkbox" name="production_post_id_${index}[]" value="${post.id}" id="production_post_${post.id}_${index}">
<label class="form-check-label" for="production_post_${post.id}_${index}">
${post.name}
</label>
</div>
<div class="mb-3">
<label class="form-label" for="production_post_quantity_${post.id}_${index}">
Quantité totale pour ${post.name} du produit_${index}
production_post_quantity_${post.id}_${index}
production_post_quantity_${index}[]
</label>
<input type="number" class="form-control" name="production_post_quantity_${index}[]" id="production_post_quantity_${post.id}_${index}" placeholder="Quantité totale">
</div>
`).join("");
.....
php code for inserting products and their production tasks :
// Insérer les détails de produits dans la table order_product_details
$product_count = count($_POST['product_name_']);
for ($i = 0; $i < $product_count; $i++) {
$product_name = $_POST['product_name_'][$i];
$product_ref = $_POST['product_ref_'][$i];
$product_attributes = $_POST['product_attributes_'][$i];
$product_comment = $_POST['product_comment_'][$i];
$product_rush = isset($_POST['product_rush_'][$i]) ? 1 : 0;
$product_template = isset($_POST['product_template_'][$i]) ? 1 : 0;
$product_production_days = (int)$_POST['product_production_days_'][$i];
$product_production_days_rush = (int)$_POST['product_production_days_rush_'][$i];
$product_custom_file_comment = $_POST['product_custom_file_comment_'][$i] ?? '';
$stmt = $pdo->prepare("INSERT INTO products (name, product_ref, product_attributes, rush_option, template_option, production_day, production_day_rush, comment, product_state)
VALUES (:product_name, :product_ref, :product_attributes, :rush_option, :template_option, :production_day, :production_day_rush, :product_comment, 7)");
$stmt->execute([
':product_name' => $product_name,
':product_ref' => $product_ref,
':product_attributes' => $product_attributes,
':product_comment' => $product_comment,
':rush_option' => $product_rush,
':template_option' => $product_template,
':production_day' => $product_production_days,
':production_day_rush' => $product_production_days_rush
]);
$product_id = $pdo->lastInsertId();
$stmt = $pdo->prepare("INSERT INTO order_products (order_id, product_id) VALUES (:order_id, :product_id)");
$stmt->execute([
':order_id' => $order_id,
':product_id' => $product_id
]);
$production_post_ids = $_POST['production_post_id_' . ($i + 1)] ?? [];
$production_post_quantities = $_POST['production_post_quantity_' . ($i + 1)] ?? [];
// Insérer les relations entre les produits et les étapes de production avec les quantités
foreach ($production_post_ids as $index => $post_id) {
$quantity = isset($production_post_quantities[$index]) ? (int)$production_post_quantities[$index] : 0;
// Vérifier que les données sont valides
if ($post_id && $quantity > 0) {
$stmt = $pdo->prepare("
INSERT INTO product_production (product_id, production_post_id, production_post_state_id, total_quantity)
VALUES (:product_id, :production_post_id, 1, :total_quantity)
");
$stmt->execute([
':product_id' => $product_id,
':production_post_id' => $post_id,
':total_quantity' => $quantity
]);
}
}
...
don’t hesitate to let me know if you’re missing any information or if there’s a problem with my code.
I tried changing the data insertion method and the form layout.
For each product, each production task must be inserted with the associated quantity entered in the total_quantity field of the product_production table.
Currently, if there are several products, this only works for the first product.