PHP - Ayuda porfa,, Insertar un foreach en mysql

 
Vista:
Imágen de perfil de Anthony

Ayuda porfa,, Insertar un foreach en mysql

Publicado por Anthony (4 intervenciones) el 13/10/2016 05:55:26
Hola Espero esten bien, tengo un problema y me urge poder salir de el para poder continuar.. estoy tratando de hacer un UPDATE en mysql atra vez de php aqui mi codigo que llevo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 
$res = "";
$str = "";
foreach($id as $k => $valorr)
{ $idd[] = $valorr;
}
$resta = array_filter($resta);
foreach($resta as $k => $valor) {
    $str .= "UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - ".$resta[$k]." WHERE ID_R = ".$idd[0].";</br>";
}
 
echo $str;
 
?>

Resultado

1
2
UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 4 WHERE ID_R = 1;
UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 11 WHERE ID_R = 1;

Resultado que Busco

1
2
UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 4 WHERE ID_R = 1;
UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 11 WHERE ID_R = 6;


el caso es que en esta parte coloco
1
WHERE ID_R = ".$idd[0]
<- el cero y me sale ID = 1 si coloco ".$idd[1] me sale el ID = 6 , como hago para que funcione automatico?.. es lo que llevo hasta ahora Saludos espero me puedan ayudar porfa
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Ayuda porfa,, Insertar un foreach en mysql

Publicado por xve (6935 intervenciones) el 13/10/2016 09:04:56
Hola Antony, puedes hacerlo de dos maneras.. .una es con una variable contadora... algo como...
1
2
3
4
$i=0;
foreach($resta as $k => $valor) {
    $str .= "UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - ".$resta[$k]." WHERE ID_R = ".$idd[$i++].";</br>";
}

la otra manera, seria utilizando un bucle for() en vez de un foreach()... algo así:
1
2
3
4
$k=array_keys($resta);
for($i=0;$i<$resta) {
    $str .= "UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - ".$k[$i]." WHERE ID_R = ".$idd[$i].";</br>";
}

No se muy bien el contenido del array, pero creo que te tiene que funcionar... si lo puedes probar y comentar...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Anthony

Ayuda porfa,, Insertar un foreach en mysql

Publicado por Anthony (4 intervenciones) el 13/10/2016 16:23:04
Hola XVE un placer para mi que me escribas he visto muchos de tus aporte y siempre das con la solución,

este es el array de como llegan

Array ( [0] => 4 [1] => 0 [2] => 11 ) Array ( [0] => 1 [1] => 6 )

<- resta .. ID ->

ire a probar tu solución
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Anthony

Ayuda porfa,, Insertar un foreach en mysql

Publicado por Anthony (4 intervenciones) el 13/10/2016 16:41:34
XVE no me funciono :(

pienso que el problema es el array ..

hay forma de agarrar este array ya pasando por array_filter para asi eliminar los cero que llegan y convertirlo en esto?

Array ( [0] => 4 [1] => 11 ) Array ( [0] => 1 [1] => 6 )

asi cada quien tendria su pareja y poder pasarlo por el ciclo ?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Ayuda porfa,, Insertar un foreach en mysql

Publicado por xve (6935 intervenciones) el 13/10/2016 19:47:57
Hola Antony, tienes razón, he visto que he cometido algunos errores...

Al ver que los dos arrays son de diferente tamaño, he tenido que hacerlo con la variable contadora... prueba este código haber si te sirve:
1
2
3
4
5
6
7
8
9
10
11
12
<?php
$resta=Array ( 0 => 4, 1 => 0, 2 => 11 );
$idd=Array (0=>1,1=>6);
 
$i=0;
foreach($resta as $k => $valor) {
    $str= "UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - ".$resta[$k]." WHERE ID_R = ".$idd[$i++].";</br>";
    echo $str;
    if($i>=count($idd))
        $i=0;
}
?>

Devuelve:
UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 4 WHERE ID_R = 1;
UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 0 WHERE ID_R = 6;
UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 11 WHERE ID_R = 1;


Coméntanos, ok?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Anthony

Ayuda porfa,, Insertar un foreach en mysql

Publicado por Anthony (4 intervenciones) el 13/10/2016 21:01:28
Hola XVE

ese metodo tampoco me funciona porque

UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 4 WHERE ID_R = 1;
UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 0 WHERE ID_R = 6;
UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 11 WHERE ID_R = 1;

si te fija deberia ser como ya comentaba arriba yo uso array_filter para eliminar los cero, y arriba estas restando el -11 al ID 1 en vez de al 6 que es a quien le corresponde.

debe quedar asi

UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 4 WHERE ID_R = 1;
UPDATE `inventario`.`registro` SET `unidades_r` = `unidades_r` - 11 WHERE ID_R = 6;

PERO EN FIN lo que hice fue esto

1
2
3
4
5
6
7
8
9
10
11
<?php
$resta = array_filter($resta);
foreach($id as $valorr)
{
echo "<input type='hidden' name='ix[]' value=".$valorr.">";
}
foreach($resta as $valor)
{
echo "<input type='hidden' name='rx[]' value=".$valor.">";
}
?>

ya que el problema de los ceros nunca me iba a dejar quieto ya que altera las clave de los arrays y nunca iban a quedar en pareja entonces tome los valores que si necesito ya pasado por array_filter asi no sale el cero, los meti en un formulario luego en un input hidden asi como muestro arriba, el formulario me lleva a otro php ahi tengo esto .

1
2
3
4
5
6
7
8
9
10
11
<?php
  $ix = $_POST['ix'];
  $rx = $_POST['rx'];
 
$str = "";
for($k=0; isset($rx[$k]); $k++) {
    $str .= "UPDATE inventario.registro SET unidades_r = unidades_r - ".$rx[$k]." WHERE ID_R = ".$ix[$k].";";
}
$sql = $str;
 
mysql_query($sql);

hice un echo "$sql"; y me regresa

UPDATE inventario.registro SET unidades_r = unidades_r - 4 WHERE ID_R = 1;
UPDATE inventario.registro SET unidades_r = unidades_r - 11 WHERE ID_R = 6;

Perfecto Pero cuando voy al MYSQL no ha echo ningun cambio .. yo pienso que es brujeria ya esto xD .. agarro los dos codigos los meto directo en mysql por la consola y si me funciona O_O? que me estas contando dios :s .. XVE porque no me funciona al mandarlo por el PHP? .. si mando uno solo

Si funciona

UPDATE inventario.registro SET unidades_r = unidades_r - 4 WHERE ID_R = 1;

pero si mando los dos no funciona no entiendo. o_o?

UPDATE inventario.registro SET unidades_r = unidades_r - 4 WHERE ID_R = 1;
UPDATE inventario.registro SET unidades_r = unidades_r - 11 WHERE ID_R = 6;

Saludos XVE :)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar