Error when insert data with javascript form

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 :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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("");
.....
</code>
<code> 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(""); ..... </code>
  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 :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// 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
]);
}
}
...
</code>
<code>// 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 ]); } } ... </code>
// 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.

Recognized by PHP Collective

7

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật