Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Have two variables in one foreach bad insert

Hello i have a trouble with my code.

I have HTML with JS:

$(document).ready(function () {
 
      // allowed maximum input fields
      var max_input = 5;
 
      // initialize the counter for textbox
      var x = 1;
 
      // handle click event on Add More button
      $('.add-btn').click(function (e) {
        e.preventDefault();
        if (x < max_input) { // validate the condition
          x++; // increment the counter
          $('.wrapper').append(`
            <div class="input-box">
              <input type="text" name="input_name[]"/>
              <input type="text" name="input_price[]">
              <a href="#" class="remove-lnk">Remove</a>
            </div>
          `); // add input field
        }
      });
 
      // handle click event of the remove link
      $('.wrapper').on("click", ".remove-lnk", function (e) {
        e.preventDefault();
        $(this).parent('div').remove();  // remove input field
        x--; // decrement the counter
      })
 
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
                        <div class="input-box">
                          <input type="text" name="input_name[]">
                          <input type="text" name="input_price[]">
                          <button class="btn add-btn">+</button>
                        </div>
                      </div>

and i need insert in DB all inputs (name and price) Now if i trying insert only first line.

php script: This is a function and $id_produkt is GET from url.

if (isset($_POST["input_name"]) && is_array($_POST["input_name"])){ 
 
 
    $input_name = $_POST["input_name"]; 
    $input_price = $_POST["input_price"]; 
  
  
    foreach (array_combine($input_name, $input_price) as $field_name => $field_price){

      $sql = "INSERT INTO variant_product ( id_product, name, price ) VALUES(?,?,?)";
      $data = array("isi", $id_produkt, $field_name, $field_price);
      
      $result = db_query($sql, $data);

      return $result;
    }
  
  }

Can help me please ? I am tired

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I make function like that and working.

function insertVariantsProduct($id_produkt){  
$userData = count($_POST["input_name"]);

if ($userData > 0) {

    for ($i=0; $i < $userData; $i++) { 
        if (trim($_POST['input_name'] != '') && trim($_POST['input_price'] != '')) {
            
            $var_id = $_POST["input_id"][$i];
            $name   = $_POST["input_name"][$i];
            $price  = $_POST["input_price"][$i];
            if(empty($var_id) && !isset($var_id)){      
                $sql = "INSERT INTO variant_product ( id_product, name, price ) VALUES(?,?,?)";
                $data = array("isi", $id_produkt, $name, $price);
            }else {
                $sql = "UPDATE variant_product SET name='$name',price='$price' WHERE id='$var_id'  ";
                $data = null;
            }
            $result = db_query($sql, $data);
            
        }
    }
    return $result; // This should be out of loop because it's will break the loop 

}

}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!