Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
Tener dos variables en una mala inserción foreach

Hola, tengo un problema con mi código.

Tengo HTML con 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>

y necesito insertar en DB todas las entradas (nombre y precio) Ahora, si intento insertar solo la primera línea.

script php: esta es una función y $ id_produkt es GET de 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; } }

¿Puede ayudarme por favor? estoy cansado

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Hago funcionar así y trabajando.

 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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda