how to make a multi update in php?

0

Good morning, I have come to tell you what I want to do and what my current code is.

It turns out that I am trying to do a multi update done, I use the "id" of the element to filter in the table and in that same element I want to modify a field. The code I have does not work for me and it took me a whole day to find a solution.

I will leave my code to help me solve it.

This is the form:

<form method="POST" action="fun/u_list_su.php">

   <input type="text" id="id_producto[]" name="id_producto[]">
   <input type="text" id="cantidad_producto[]" name="cantidad_producto[]">
   <button type="submit">Save</button>
</form>

This is the code:

<?php
  //Aqui va la conexión
  $mysqli = new mysqli('localhost', 'usuario', '123456', 'mybd');
  if($mysqli->connect_error){

    die('Error en la conexion' . $mysqli->connect_error);

  }
   //A partir de acá es el codigo que tengo
  if (isset($_POST['id_producto'])) {
    $id_el=$_POST['id_producto']; 
    $cant_el=$_POST['cantidad_producto'];
    $sql2 = '';

    for ($count=0; $count <count($id_el) ; $count++) { 
        $id_el_clean = $mysqli->query($id_el[$count]);
        $cant_el_clean = $mysqli->query($cant_el[$count]);
        if ($id_el_clean != '' && $cant_el_clean != '') {
            $sql2 .= "UPDATE products SET stock = -'$cant_el_clean' WHERE id_producto = '$id_el_clean'";
        }

    }
    if ($sql2 != '') {
        if ($mysqli->query($sql2)) {
            echo "data insert";
        }
        else {
            echo "Error";

        }
    }
    else {
        echo "Los espacios requeridos";

    }
}
?>

Thanks for your help!

    
asked by Yadir Ortega Ramirez 01.09.2017 в 23:43
source

1 answer

0

I have attached an indicative code:

$paises = array();
$pais['id'] = 1;
$pais['nombre'] = "Argentina";
array_push($paises, $pais);

$conn = new mysqli("localhost", "root", "", 'base_de_datos');

if (!$conn) {
die("Conexion con MySQL invalida: " . mysqli_connect_error());
}
else{
echo "Conexion con MySQL correcta";
}
if(is_array($paises)){

$sql = "INSERT INTO paises (id, nombre_pais) values "; // Genero la estructura de la consulta.

$valuesArr = array(); //Vector auxiliar.
foreach($paises as $row){ //Recorro el vector principal.
    $id = (int)($row['id']);
    $nombre = mysql_real_escape_string($row['nombre']);

    $valuesArr[] = "('$id', '$nombre_pais')";
}

$sql .= implode(',', $valuesArr);

mysqli_query($conn, $sql) or exit(mysqli_error($conn)); //Ejecuto la consulta.
}
    
answered by 02.09.2017 / 01:14
source